memory impact of an import statement for a setup file in classes as opposed to a setup class passed as an instance parameter #10279
Replies: 4 comments 1 reply
-
One option is to put your setup data into a d = {'s':4, 'a':1, 'z':42, 'b':2}
def f(a, b, **_): # Discard unused args
print(a, b)
f(**d) # f() takes only those args that it needs |
Beta Was this translation helpful? Give feedback.
-
If the dict is that big I would consider using a btree database located in flash storage. That way very little RAM would be used. Each class would retrieve the parameters it needs from the database. |
Beta Was this translation helpful? Give feedback.
-
Just for what it's worth, and it's only my personal opinion: |
Beta Was this translation helpful? Give feedback.
-
The if setup.py is just use_display = True
...
tv_ops_led = {
'led_max_on_timer': -1,
...
}
... then in all your other files using import setup
...
...
def foo():
if (setup.use_display):
... will be the most efficient way to achieve what you want. (Unless you do what Peter suggested with btree, where the actual variable storage lives in flash, although if you're accessing variables a lot, you'll have other overheads including more allocations at run time). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello Everyone,
I really hope I can explain my question, so please bear with me.
I have a couple of classes that control various aspects of a diorama such as Lights, Doors a PIR receiver and an OLED display. Each class needs some configuration parameters so I created a setup.py file with a setup class in it that holds all static configuration. This class is instantiated in the main.py and passed as an instance variable to the instantiation of the door class, the lights class etc.
Now I am wondering: From a memory consumption perspective is it better to initiate one setup class in main and pass it as a class instance to all other classes (such as the named door or lights class)? Or would it be better to simply do an import of the setup.py-file in each other class and query the necessary parameters there? Given that most of the parameters are of a static nature it feels more correct to have these parameters as class variables instead of instance variables which they obviously are when I pass the setup class on instantiation. But as said, I am mostly concerned about the memory impact the instantiated and passed along setup class has as opposed to an import statement in each class.
I know that where a lot of classes in this question, but can anyone give me a hint here? Any best practises for class and instance variables and memory impact of import statement vs. passed along setup class?
Thanks in advance,
Christian
Beta Was this translation helpful? Give feedback.
All reactions