Python with real habits lesson plan
Advanced Python on a Raspberry Pi, after Python from zero: modules, classes, exceptions, files and logging, tests with plain assert, generators, and a sensor logger that uses them all.
Objectives and materials
By the end, students can
- Split code into modules, and bundle data with its functions in classes.
- Exceptions, files and logs: code that survives a loose wire and tells you what happened.
- Tests with assert, and generators that stream data forever.
- A sensor logger with a screen, a CSV and a log, built from every habit in the course.
Materials for the whole unit
- Eight-channel ADC, MCP3008
- Half breadboard
- LED
- OLED display, 128x64
- Potentiometer
- Pressure, humidity and temperature, BME280
- Raspberry Pi 4 Model B
- Raspberry Pi 5
- Raspberry Pi Zero 2 W
- Resistor
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.
Structure
Split code into modules, and bundle data with its functions in classes.
| Lesson | Time | What students do |
|---|---|---|
| ModulesSplit a program into files: the OLED driver you have been importing all along, the standard library, a module of your own in a new tab, and what __name__ == "__main__" is for. | 50 min |
|
| ClassesClasses and objects on a BME280: the driver class you already use, a Reading class of your own with methods and __str__, an object that remembers the highest temperature, and inheritance. | 50 min |
|
Materials
- OLED display, 128x64
- Pressure, humidity and temperature, BME280
- Raspberry Pi 4 Model B
- Raspberry Pi Zero 2 W
Check for understanding
Each student finishes "Classes" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "When to write a class".
When things go wrong
Exceptions, files and logs: code that survives a loose wire and tells you what happened.
| Lesson | Time | What students do |
|---|---|---|
| ExceptionsRead a traceback, catch the I2C error a missing chip really raises, scan two addresses without crashing, raise an exception of your own, and clean up with else and finally. | 50 min |
|
| Files and loggingWrite a knob's readings to a file and read them back, a CSV any spreadsheet opens, the logging module with its levels in place of print, and a log file: what the Pi's engine supports, and what lasts. | 50 min |
|
Materials
- Eight-channel ADC, MCP3008
- Half breadboard
- LED
- Potentiometer
- Pressure, humidity and temperature, BME280
- Raspberry Pi 5
- Raspberry Pi Zero 2 W
- Resistor
Check for understanding
Each student finishes "Files and logging" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What lasts, and what does not".
Craft
Tests with assert, and generators that stream data forever.
| Lesson | Time | What students do |
|---|---|---|
| Tests with assertNo pip and no pytest, so tests are plain assert: catch a real off-by-one in a voltage conversion, test the edges, write a ten-line test runner, and let a failing test drive a fix. | 50 min |
|
| Generatorsyield and next, a generator that streams knob readings forever, a moving average built as a pipeline of generators, islice to take just a few, and generator expressions. | 50 min |
|
Materials
- Eight-channel ADC, MCP3008
- Half breadboard
- LED
- Potentiometer
- Raspberry Pi 5
- Resistor
Check for understanding
Each student finishes "Generators" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Why generators".
Boss project
A sensor logger with a screen, a CSV and a log, built from every habit in the course.
| Lesson | Time | What students do |
|---|---|---|
| Project: a sensor loggerThe boss project: a BME280 and an OLED on one I2C bus, a logger module with a Sample class and a generator of samples, a test before anything is trusted, a CSV log, the latest reading on the screen and a warning when it gets hot. | 60 min |
|
Materials
- OLED display, 128x64
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W
Check for understanding
Each student finishes "Project: a sensor logger" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What you built".
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.