Lesson plan

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.

4 chapters, 7 lessons, about 5 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

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

Chapter 1. About 1 h 30 min

Tools

Scripts with real options, and changing a folder of files without fear.

LessonTimeWhat 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
  1. What a script is given
  2. A parser
  3. Arguments that do something
  4. --help for free
  5. Clear errors
  6. A command line in the Serial Monitor
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
  1. A messy folder
  2. Plan the new names
  3. Find the conflicts
  4. Dry run, then rename
  5. An undo log

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

Chapter 2. About 1 h 20 min

Logs and time

Know what your script did, and run it on time.

LessonTimeWhat 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
  1. Levels
  2. Filter by level
  3. Named loggers
  4. A log file
  5. Tracebacks in the log
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
  1. The drifting loop
  2. A fixed-rate schedule
  3. Several jobs
  4. At a time of day
  5. Stop cleanly

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

Chapter 3. About 1 h 20 min

Pipelines

React to files as they arrive, and turn data into reports.

LessonTimeWhat 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
  1. An instrument that drops files
  2. Spot new files
  3. Process and move
  4. Bad files go aside
  5. A running count
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
  1. The experiment
  2. Group into means
  3. A Markdown table
  4. Highlights
  5. Fill in a message

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

Chapter 4. About 40 min

Boss

A lab notebook bot built from everything in the course.

LessonTimeWhat 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
  1. The notebook
  2. measure, with options
  3. Bad arguments do not kill the bot
  4. report
  5. The alarm

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

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.