Need some help how to distribute features between main.py and scripts on sd card

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Need some help how to distribute features between main.py and scripts on sd card

Post by liudr » Sun Aug 19, 2018 9:45 pm

Christian Walther wrote:
Sat Aug 18, 2018 9:05 pm
...Since you don’t have great control over when a module runs (at first import), it typically shouldn’t do anything else than define functions and classes, and possibly initialize some things. Any code that needs to run at a determined time should be inside those functions and classes.
I got it now. Thanks.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Need some help how to distribute features between main.py and scripts on sd card

Post by pythoncoder » Mon Aug 20, 2018 8:08 am

No. Have the module with the logging function run

Code: Select all

import os
This forum is to support MicroPython: users assume a knowledge of Python. Your queries are elementary ones about the Python language. I would advise you to learn the language using Python on a PC with a book or online course.
Peter Hinch
Index to my micropython libraries.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Need some help how to distribute features between main.py and scripts on sd card

Post by liudr » Mon Aug 20, 2018 4:49 pm

pythoncoder wrote:
Mon Aug 20, 2018 8:08 am
No. Have the module with the logging function run

Code: Select all

import os
This forum is to support MicroPython: users assume a knowledge of Python. Your queries are elementary ones about the Python language. I would advise you to learn the language using Python on a PC with a book or online course.
That was the first thing in my mind. Unfortunately, I have not found evidence during my python book/doc reading that duplicate imports at the main and inside another module is not going to 1) waste memory by creating duplicate code/variables 2) cause hardware to lockup since the resource is a single SD card. I know that "if it works it must be OK". But that's not my programming motto. I'm going to do more investigation on the python import mechanism but meanwhile I'm seeking examples and pointers from more experienced python programmers on how to build a project in general.

In C, we use conditional pre-compiler directives to ensure the #include doesn't duplicate names in the final project when multiple source files rely on the same #include. It would otherwise result in some "first declared here" errors. I was able to improve my understanding of the compiling and linking process in C when I encountered the same issue, managing multiple source files as my projects grew longer. That was ~20 years ago. With Python, I was able to develop projects of sufficient complexity all inside a single script. This means my syntax is mostly solid enough. I am at the same point to develop my own modules and understand the impact of importing other modules. I can't visualize this process as I could with C. Maybe C is just less complex. Python seems more complex. What I figured out (may be wrong) is that if there is code in an imported module, the code runs in its own namespace, isolated from the importer. So I should enclose all code in functions so they only run AFTER import is over. It's taking more time than I anticipated for me to get the gist of the stuff. I'll try to keep things MicroPython-related instead of Python-related.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Uused imports

Post by pythoncoder » Tue Aug 21, 2018 7:45 am

Unused imports can easily creep into code, for example you might import time to test a function's execution time, then remove the test. So before releasing code I check for and remove unused imports. They waste RAM and make code that bit harder to follow.

As for crashing hardware
since the resource is a single SD card
I don't follow that reasoning. Normal Python code should never crash hardware, and this includes multiple or spurious imports. Serous abuse of uctypes or monkeying around with the inline assember aside, if MicroPython crashes the hardware you've probably found a language bug.

Python is quite unlike C in that respect.
Peter Hinch
Index to my micropython libraries.

Post Reply