List of sections within microbit module?

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
Siriushardware
Posts: 20
Joined: Wed Nov 01, 2017 8:00 pm

List of sections within microbit module?

Post by Siriushardware » Wed Nov 01, 2017 9:06 pm

Most Micropython code examples start with the line

from microbit import *

...followed by a few lines of code which don't come anywhere close to filling up the limited code memory space of the Micro:Bit.

My understanding (which could be wrong) is that importing * results in all of the support code for all of the standard features of the Micro: Bit being included in your output code, even if your program does not use ninety percent of it.

I've already run into problems with this, having written some code which, together with the full imported block of support code, managed to exceed the size of the available code memory.

The solution to this would seem to be to import only those parts from microbit which my code needs, but, here's my problem, I can't find a list of Microbit sub-modules anywhere. I therefore don't know which modules to import and which to leave out. In fact, I don't even know their names.

In the case of the over-ambitious project referred to, I correctly guessed that one of the sub-parts would be called 'display' and so I replaced

from microbit import *

with

from microbit import display

This was accepted, but then the upload threw up an error at the first line containing a pinx.digital_write (n) statement, so presumably there's also an I/O subsection in microbit which I have to import as well, in order for pin read / pin write statements to work. But what is it called, and where can I find the names and details of all the other 'importable' parts of 'microbit' so I can choose which ones to import and which ones to leave out?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: List of sections within microbit module?

Post by deshipu » Thu Nov 02, 2017 12:50 am

To be able to use any name, you have to import it. To use "pin0" you have to do "from microbit import pin0". There is no subsection responsible for io, it's just names.

As for conserving memory, your reasoning sounds sensible, but unfortunately is wrong. All the built-in modules reside in flash in the firmware, and don't actually take any additional RAM when you import them – they are executed directly from the firmware. It doesn't matter if you are using them or not, they use up the same amount of memory.

There is, however, a small difference between using "from microbit import *", "from microbit import pin0, pin1, display" and "import microbit" and then using "microbit.pin0" etc. — the first two commands will create local variables in your program, and thus use up some extra RAM for the variable names and values in the local dict, while the last one will refer directly to the variables that already exist in the firmware, and use no extra memory.

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

Re: List of sections within microbit module?

Post by pythoncoder » Thu Nov 02, 2017 8:06 am

@Siriushardware You can see the effects of different ways of importing libraries with Python's dir() command.

Code: Select all

>>> import utime
>>> dir()
['__name__', 'utime']
>>> dir(utime)
['__name__', 'clock', 'sleep', 'sleep_ms', 'sleep_us', 'time', 'ticks_ms', 'ticks_us', 'ticks_cpu', 'ticks_add', 'ticks_diff', 'localtime']
The second listing shows the contents of the utime library and confirms that you can access, for example

Code: Select all

utime.ticks_ms()
The effect of importing * is shown in the following session (with MicroPython restarted):

Code: Select all

>>> from utime import *
>>> dir()
['localtime', 'ticks_add', 'ticks_cpu', 'ticks_ms', 'time', 'clock', '__name__', 'sleep_us', 'sleep', 'ticks_diff', 'ticks_us', 'sleep_ms']
>>> 
As a very general point importing * should only be done when a module has been specifically designed for this purpose. This is because it can redefine symbols you have already defined in code or even Python built-in functions. Believe it or not I have actually encountered the latter in a commercial library, a bug which took some finding...
Peter Hinch
Index to my micropython libraries.

Siriushardware
Posts: 20
Joined: Wed Nov 01, 2017 8:00 pm

Re: List of sections within microbit module?

Post by Siriushardware » Thu Nov 02, 2017 7:49 pm

Gents, thanks for taking the time to answer my query in such detail. The overflow which prompted my question was, in part, due to my apparently having to individually bit-bang every part of the scanning of a matrix keypad, therefore a block of code to detect each possible row-column connection. The reply to my other post regarding the use of variables to define which pin a digital_read or digital_write comes from or goes to will allow me to make the code a lot more compact.

Post Reply