Lesson plan

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.

4 chapters, 7 lessons, about 6 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

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.

Chapter 1. About 1 h 40 min

Structure

Split code into modules, and bundle data with its functions in classes.

LessonTimeWhat 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
  1. Run it: a module you did not write
  2. The standard library
  3. Write your own module
  4. Use it on the screen
  5. Who is __main__?
  6. Where Python looks
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
  1. Run it: an object from a class
  2. Write a class
  3. Teach it to print itself
  4. An object that remembers
  5. Inheritance: share what is the same
  6. When to write a class

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

Chapter 2. About 1 h 40 min

When things go wrong

Exceptions, files and logs: code that survives a loose wire and tells you what happened.

LessonTimeWhat 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
  1. Run it: a crash, on purpose
  2. Catch it
  3. Try each address
  4. Raise your own
  5. else and finally
  6. Catch narrow, crash loud
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
  1. Run it: write a file
  2. Read it back
  3. CSV: rows and columns
  4. logging instead of print
  5. Log to a file
  6. What lasts, and what does not

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

Chapter 3. About 1 h 40 min

Craft

Tests with assert, and generators that stream data forever.

LessonTimeWhat 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
  1. Run it: a test that fails
  2. Fix the bug, watch it pass
  3. Test the edges
  4. A test runner in ten lines
  5. A failing test drives a fix
  6. Habits worth keeping
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
  1. Run it: yield
  2. next, and the end
  3. A stream that never ends
  4. A pipeline: generators feeding generators
  5. Take a few, and generator expressions
  6. Why generators

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

Chapter 4. About 1 h

Boss project

A sensor logger with a screen, a CSV and a log, built from every habit in the course.

LessonTimeWhat 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
  1. Run it: two chips, one bus
  2. A module: Sample and samples()
  3. Test before you trust
  4. Log to CSV
  5. Show it, and warn when hot
  6. The screen shows the latest
  7. What you built

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

Your class

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.