#๐Ÿ”’ Simulation code programming pattern guidance

12 messages ยท Page 1 of 1 (latest)

thick egret
#

I am looking for some help on organizing some code for a physics simulator. I want to know if this design structure is "ok" , if theres a name for it, or any better ways to structure my code.

Basically my code is composed of many components that eventually get called in essentially a pipeline: basically one MySim.run at the end of the day. I rely on duck typing checked by protocols to swap out components where appropriate. I treat each class as an immutable function call. Most of the classes are dataclasses, but not necessarily.

My Concern is that it feels weird to simply make these objects to use once and throw away. I am also just looking to see if this pattern has a name so I can go find other implementations and maybe find some blindspots.

It does seem like ETL: my concern with ETL libraries is it feels like they simply invented a new programming language in JSON or whatever that I rather not bother with.

@dataclass
class SubComponentA:
    size: float
    weight: float
    def run_component(self, a, b, c):
        ...
        
@dataclass
class SubComponentB:
    temperature: float
    speed: float
    def run_component(self, x, y):
        ...
        
@dataclass
class ComponentA:
    heavy_thing: SubComponentA
    fast_thing: SubComponentB
    def run_component(self, a, b, c):
        x, y = self.heavy_thing.run_component(a, b, c)
           return self.fast_thing.run_component(x, y)
    
@dataclass
class ComponentB:
    ...
    
@datclass
class MySim:
    part_A: ComponentA
    part_B: ComponentB
    def run(self, a, b, c):
        ... = self.part_A.run_component(a, b, c)
        return self.part_B.run_component(...)
languid gullBOT
#

@thick egret

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sinful trench
#

I believe the pattern you are referencing is called a singleton

thick egret
#

It feels like i am basically using the class constructor to curry a more elaborate function.

#

i guess at the top level it is indeed a singleton, but in general I could have multiple versions of SubcomponentA for example

sinful trench
#

are you familiar with other programming paradigms?

#

depending on the problem a different programming paradigm could be better suited

thick egret
#

Do you mean functional vs object oriented vs procedural?

#

im trying to place what i am doing... I feel like its functional, but i am just using classes to set up higher order functions

#

but then i have a lot of these "dumb" classes that are basically run this, do that, run this, execute that

languid gullBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.