Lite / Intelligent Robotic Systems / Week 1
One script is not a system
A rover planner works perfectly as one 600-line file. Then you change a unit conversion and three unrelated features break. Why?
Why should I care?
Real robot software is hundreds of files written by many people. It stays workable only because each file (a module) does one job and talks to the others through a few well-named functions. When a module breaks, the dependency graph tells you exactly what else might have broken with it.
- Split a program into modules, each with one job and a small public interface.
- Import names from your own modules and explain how Python finds them.
- Trace which modules must be re-tested when one module changes, and spot a circular import.
Predict
In a planner package, units.py is imported by grid.py and report.py; grid.py is imported by planner.py and io_csv.py; main.py imports io_csv, planner and report. You change units.py. How many other modules could be affected?
- Only the two that import it directly
- All five modules that import it directly or indirectly
- None, if you only changed one function
Guess first, then reveal
Five. grid.py and report.py import it directly, but planner.py and io_csv.py depend on grid.py, and main.py depends on all of them. A change ripples up every arrow that leads to the changed module, which is why low-level modules deserve the most tests.
Discover
Select units.py, then grid.py, then main.py and count the orange modules each time. Which module is safest to change, and which is riskiest? Now tick the shortcut that lets grid.py import planner.py and read the warning.
What the interactive shows (text version)
The dependency explorer shows seven modules in layers: units.py at the bottom; grid.py and report.py import units; io_csv.py and planner.py import grid; main.py imports io_csv, planner and report; test_planner.py imports planner and grid. Changing units.py affects all five modules above it. Ticking the shortcut makes grid.py also import planner.py, creating a circular import.
Watch (optional)
Watch for: Watch for the difference between import module and from module import name, and where Python searches for your own files.
Shows how import finds a module, how to import only the names you need, and where Python looks for your own files.
“Python Tutorial for Beginners 9: Import Modules and Exploring The Standard Library” by Corey Schafer (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
A module is just a .py file
Any file units.py is a module called units. Another file in the same folder can write import units and call units.cm_to_m(250), or from units import cm_to_m to use the name directly. Python searches the folder of the script you ran first, then the standard library and installed packages.
Interfaces, not internals
Each module should expose a few functions or classes (its interface) and keep its helpers private. By convention, a name starting with an underscore, such as _clamp, is internal. Other modules use only the interface, so you can rewrite the inside freely as long as the interface and its tests still hold.
Arrows only go one way
Draw an arrow from each module to the modules it imports. Good packages form layers: small utility modules at the bottom, the program's entry point at the top, and no loops. A circular import (A imports B and B imports A) means neither can be loaded or tested on its own; fix it by moving the shared code down a layer or the calling code up.
Explain it another way
Intuitive
Think of a team building a robot. The motor team promises 'give us a speed in rpm and we will spin at it'. The navigation team doesn't need to know how the motor driver works, only that promise. Modules are those promises written in code.
Technical
import x runs x.py once, caches the module object in sys.modules and binds the name x. Later imports reuse the cache. Code at the top level of a module runs at import time, so modules should mostly define functions and classes, with any script behaviour guarded by if __name__ == "__main__":.
Applied
The Robot Operating System (ROS) splits a robot into packages and nodes with declared dependencies, so a team can rebuild and test only the parts affected by a change. Your four-file planner is the same idea at a smaller scale.
Worked example 1
Split this conversion out of a planner script into its own module, then use it.
# file: units.py
"""Unit conversions shared by the rover planner."""
CM_PER_M = 100
def cm_to_m(cm):
"""Convert a length in centimetres to metres."""
return cm / CM_PER_Mfrom units import cm_to_m
print(cm_to_m(250)) # 2.5The second file only knows the name cm_to_m and its promise. If you later change how the conversion is done, nothing else needs to change.
Common mix-ups
Splitting code into files makes it slower.
✓ Imports happen once and are cached; the cost is negligible. The benefit is code you can understand, test and change in pieces.If it runs, the structure doesn't matter.
✓ Structure decides how much breaks when you change something. One-way dependencies keep that blast radius small.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Take a script you wrote earlier (or the starter planner in the Code Lab) and split it into at least three modules: one for unit conversions, one for loading data and one for the calculation. Draw the dependency arrows on paper. Check there are no loops, and list which modules you would re-test if each one changed.
Reflect
Which module in your split would you protect with the most tests, and why?
Write your answer on paper or in your own notes. Lite never stores what you write.
Go further
- Python tutorial: Modules
The official explanation of import, the module search path and packages.