Skip to content
Tactiverge

School/○ Innovator/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?

About 35 min · 3 goals · Checked by automated tests

1.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.

2.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.

Loading the interactive…

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

units.py defines cm_to_m. Which line lets another file call cm_to_m(250) with no prefix?

4.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?

Guess first. Being wrong here is how the surprise works.

5.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

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.

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_M
from units import cm_to_m

print(cm_to_m(250))   # 2.5

The 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.

6.Watch

→ 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.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2

In a package with no circular imports, which module's changes need the MOST re-testing elsewhere?

Practice 3

grid.py imports planner.py so GridMap can call bfs, and planner.py imports grid.py. What is the best fix?

Practice 4

planner.py contains def _neighbours_in_bounds(...). What does the leading underscore tell other programmers?

Practice 5

io_csv.py ends with print(load_map('mars.csv')) at the top level. What happens when planner tests run 'import io_csv'?

8.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.

9.Reflect

10.Go further

Concepts: Functions and decomposition · Robot system architecture