Python modules and timing
Python libraries are arranged as modules, containing a number of functions and/or classes. These modules must be
imported before being used. You can either import the complete module, or you can import individual functions or classes to be used.
As an example, let's study the
time module. In MicrroPython the module is called utime (for micro-time) and it contains functions allowing us to delay execution for a certain amaount of time.
- sleep(t) sets the program to sleep for t seconds
- sleep_ms(t) sets the program to sleep for t ms
If, in our program which prints "Hello World!" we want to have the text printed only every second we could program this as follows:
import utime # import time would work as well
for i in range(5):
print("Hello World")
utime.sleep(1) # delay execution be 1 s, utime.sleep_ms(1000) would do the same. sleep_ms is not available in CPython
The other option is to import just the function to be used:
--
Uli Raich - 2022-10-15
Comments