Lesson plan

Files, CSV and JSON lesson plan

Build the flight computer of a weather balloon: read and write files without losing them, log a live sensor to CSV, keep settings in JSON, organize folders, survive every disk error, and ship a sensor log tool.

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

  • Read a flight plan, then write a log that survives a power cut.
  • Rows of readings in CSV, settings in JSON.
  • Organize the files, and keep going when the disk says no.
  • A sensor log tool for launch day.

Materials for the whole unit

  • Pressure, humidity and temperature, BME280
  • Raspberry Pi Zero 2 W

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

Text files

Read a flight plan, then write a log that survives a power cut.

LessonTimeWhat students do
Reading filesRead a balloon's flight plan from a text file: the whole file, its lines and their hidden newlines, a loop that skips comments, a key: value parser, and the bytes underneath the text.45 min
  1. Open and read
  2. Lines and the hidden newline
  3. Loop over the lines
  4. Parse key: value
  5. Bytes underneath
Writing safelyWrite a flight log without losing it: "w" that wipes the file, "a" that appends, newlines you have to add, print to a file, and an atomic save with os.replace that survives a power cut.45 min
  1. Write a file
  2. "w" wipes the file
  3. "a" appends
  4. print() into a file
  5. Survive a power cut

Materials

  • Pressure, humidity and temperature, BME280
  • Raspberry Pi Zero 2 W

Check for understanding

Each student finishes "Writing safely" 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: "Survive a power cut".

Chapter 2. About 1 h 30 min

CSV and JSON

Rows of readings in CSV, settings in JSON.

LessonTimeWhat students do
CSV in and outRead a recorded balloon flight with DictReader, turn pressure into altitude, log the live BME280 to a CSV with csv.writer, append with a header only once, and see how commas and quotes inside values survive.45 min
  1. Read a flight
  2. Pressure into altitude
  3. Log the live sensor
  4. Append, header once
  5. Commas inside values
JSON configKeep a flight computer's settings in config.json: load it, fill gaps from defaults, validate it with clear messages, save it back atomically with indent and sort_keys, and learn what JSON changes on the way through.45 min
  1. Load config.json
  2. Defaults for what is missing
  3. Validate before you fly
  4. Save it back
  5. What JSON changes

Materials

  • Pressure, humidity and temperature, BME280
  • Raspberry Pi Zero 2 W

Check for understanding

Each student finishes "JSON config" 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: "What JSON changes".

Chapter 3. About 1 h 30 min

Folders and failures

Organize the files, and keep going when the disk says no.

LessonTimeWhat students do
Paths and foldersOrganize a flight computer's files: the working folder, folders by date with makedirs, numbered log names that never overwrite, pathlib's Path objects, and a walk over the whole tree with sizes.45 min
  1. Where am I?
  2. Folders by date
  3. Names that never overwrite
  4. pathlib
  5. Walk the tree
Errors on diskMake a flight computer that keeps going when files go wrong: a missing config, a config cut off by a power failure, folders where files should be, bad rows in a CSV, try/except/else/finally, and the OSError family by errno.45 min
  1. The missing file
  2. A damaged file
  3. Folders where files should be
  4. Bad rows, else and finally
  5. The OSError family

Materials

  • Pressure, humidity and temperature, BME280
  • Raspberry Pi Zero 2 W

Check for understanding

Each student finishes "Errors on disk" 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 OSError family".

Chapter 4. About 45 min

Project

A sensor log tool for launch day.

LessonTimeWhat students do
Project: a sensor log toolSkylark's ground tool, from everything in the course: a config with defaults, numbered CSV logs of the live BME280 in a folder, a list of logs with rows and sizes, a summary saved as JSON atomically, and temperature alarms from the config.45 min
  1. The tool starts
  2. log N
  3. list
  4. summary
  5. Alarms from the config

Materials

  • Pressure, humidity and temperature, BME280
  • Raspberry Pi Zero 2 W

Check for understanding

Each student finishes "Project: a sensor log tool" 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: "Alarms from the config".

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.