Module Import Flow
Run the Module Import Flow MicroSim Fullscreen
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
About This MicroSim
Every .py file on your robot is a module, which is a file of code that
other files can use. The import statement links one module to another. This
MicroSim shows three files from the chapter:
- config.py (blue) holds the pin numbers as UPPERCASE constants, such as
RIGHT_FORWARD_PIN = 11. - motors.py (green) runs
import configand usesconfig.RIGHT_FORWARD_PINto set up the motor pin. - main.py (orange) runs
import configandimport motors, prints the pin, and callsmotors.stop_all().
Arrows show which file imports which. Every spot that reads
config.RIGHT_FORWARD_PIN is highlighted in yellow, with an orange badge that
shows its current value. The small wiring picture on the left shows which GP
pin the right forward motor wire is plugged into.
When you change the pin in config.py, every highlighted spot follows along
at once. You edit 1 line. Turn on Hard-code pins instead and the same
number is typed by hand in 5 places. Now changing config.py does
nothing. The code still drives GP11, and the wiring picture shows the motor
as not connected.
How to Use
- Press Run main.py. The console prints the import order one step at a
time, and each import arrow pulses gold as it runs. Notice that
config.pyloads only once, even though two files import it. - Change RIGHT_FORWARD_PIN to 13. Watch the badges in
motors.pyandmain.pychange, and the orange wire move to GP13. - Check Hard-code pins instead (no config.py). Change the pin again and read the Pin mismatch message and the Lines to edit counter.
- Press Reset to return to pin 11 with the
config.pypattern.
This MicroSim goes with the modular programming section of Chapter 5: Data Structures, Modular Programming, and Version Control.
Lesson Plan
Grade Level
Grades 8–12 (introductory programming with a physical robot)
Duration
15 minutes
Prerequisites
- Importing built-in modules with
import timeandfrom time import sleep(Chapter 4: Control Flow, Functions, and Exception Handling) - Defining and calling functions (Chapter 4)
- Variables and constants (Chapter 3: MicroPython and Development Environment Setup)
Learning Objective
Students will be able to explain (Bloom's Taxonomy: Understand) how
import links files together, and predict which files change behavior when
one value in config.py changes.
Activities
- Trace the imports (4 min). Students press Run main.py and write down
the order of the console lines. Ask why
config.pyloads only once. - Predict the change (3 min). Before changing the pin to 13, students list every place in the three files that will show 13 afterward. They check their list against the yellow badges.
- Break the pattern (4 min). With Hard-code pins instead on, students change the pin to 13 and count the stale numbers. They explain, in two sentences, why the motor would not move on the real robot.
- Connect to the kits (4 min). Students open a
config.pyfrom one of the robot kits and find two other constants that other files import.
Assessment
- Challenge: You move the right forward motor wire from pin 11 to pin 13.
How many lines must you edit with the
config.pypattern, and how many with hard-coded pins? Answer: 1 line withconfig.py, 5 lines when hard-coded. Students check the Lines to edit counter. - Exit ticket: "A teammate changes a pin number in
motors.pyinstead ofconfig.py. What problem might this cause later?" - Rubric (4-point): Exemplary explains that importing shares one
definition across files, predicts every file affected by a change, and
connects the pattern to separation of concerns. Proficient explains the
import link and predicts the affected files. Developing knows
config.pyholds pins but cannot explain how other files see the change. Beginning believes each file needs its own copy of the pin numbers.
References
- Chapter 5: Data Structures, Modular Programming, and Version Control - the
config.pypattern and themotors.pymodule example. - Python Tutorial: Modules - official explanation of modules and the
importstatement. - MicroPython machine.Pin - the
Pinclass used to set up the motor pin. - Separation of concerns (Wikipedia) - the design idea behind keeping hardware settings in one file.
- Magic number (programming) (Wikipedia) - why unexplained numbers typed in many places cause bugs.