Micropython – Calling modules/functions known only at runtime

One thing I need is a scheduler to run after boot.py and run functions in various files at predetermined times. I figured I could write the schedule in JSON, read it into a dictionary, and then call things as needed. There are two ways to do this:

module = __import__('foo')

fnc = getattr(module,'bar')

The first pair of lines define ‘module’ as whatever the string is, in this case a file called ‘foo.py’, and then calls the bar() function. This is handy if the function name is known prior to runtime.

The next pair of lines allow for even the function name within the file to be specified by a string.


Next: Scheduling…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.