Automate it with Python lesson plan
Let Python do a lab's chores: command-line tools with argparse, safe bulk renaming, logging, schedules that keep time, a watched inbox folder, reports written for you, and a lab notebook bot with a live temperature sensor.
Objectives and materials
By the end, students can
- Scripts with real options, and changing a folder of files without fear.
- Know what your script did, and run it on time.
- React to files as they arrive, and turn data into reports.
- A lab notebook bot built from everything in the course.
Materials for the whole unit
- Half breadboard
- LED x 2
- Pressure, humidity and temperature, BME280
- Pushbutton
- Raspberry Pi 4 Model B
- Raspberry Pi Zero 2 W
- Resistor x 2
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Tools
Scripts with real options, and changing a folder of files without fear.
| Lesson | Time | What students do |
|---|---|---|
| Scripts and argparseTurn a program into a command-line tool: sys.argv, an argparse parser with a positional, options, types, choices and defaults, a real LED blinked by the arguments, --help for free, clear errors, and arguments typed live in the Serial Monitor. | 50 min |
|
| Bulk renamingRename a folder of microscope photos safely: a regular expression that reads messy names, a plan before any change, conflicts found before they destroy a file, a dry run, the real rename, and an undo log in JSON. | 40 min |
|
Materials
- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Bulk renaming" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "An undo log".
Logs and time
Know what your script did, and run it on time.
| Lesson | Time | What students do |
|---|---|---|
| LoggingReplace print with logging: five levels, a format with the level and logger name, levels that filter, named loggers per part of a program, a log file that keeps only what matters, and tracebacks with log.exception. | 40 min |
|
| Scheduling on the clockRun jobs on time: a sleep loop and the drift it builds up, a fixed-rate schedule that does not drift, several jobs at different rates, the next 09:00 worked out with datetime, and a clean stop with Ctrl-C. | 40 min |
|
Materials
- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Scheduling on the clock" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Stop cleanly".
Pipelines
React to files as they arrive, and turn data into reports.
| Lesson | Time | What students do |
|---|---|---|
| Watching a folderProcess files the moment an instrument drops them: a button that writes sample files, a polling watcher that spots new names, processing and moving each file so it is done once, bad files sent to an errors folder, and a running count. | 40 min |
|
| ReportsTurn a class experiment's CSV into a report nobody has to type: load and convert, group into means, write a Markdown table to report.md, add highlights, and fill a message from a string.Template. | 40 min |
|
Materials
- Half breadboard
- LED x 2
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Reports" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Fill in a message".
Boss
A lab notebook bot built from everything in the course.
| Lesson | Time | What students do |
|---|---|---|
| Boss: a lab notebook botA bot that keeps the lab's notebook: notes with time stamps in Markdown, measurements from a live temperature sensor on a fixed-rate schedule with argparse options, bad arguments logged instead of crashing, a report, and a logged alarm. | 40 min |
|
Materials
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W
Check for understanding
Each student finishes "Boss: a lab notebook bot" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "The alarm".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.