Lesson plan

Text and regular expressions lesson plan

Strings, split and join, then regular expressions from search to named groups, on a rocket launch log and a ship's GPS feed, ending in a log analyzer on a Raspberry Pi.

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

  • What a string really holds, and how to cut a line into fields.
  • Describe text instead of spelling it out, and pull out the pieces.
  • A launch log and a ship's GPS, turned from text into data.
  • A log analyzer that answers commands and lights an alarm.

Materials for the whole unit

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • 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 20 min

Text, cut and glued

What a string really holds, and how to cut a line into fields.

LessonTimeWhat students do
Strings deepOne line of a rocket launch log, taken apart: repr and strip, indexing and slicing, find, in and count, why strings never change, format specs for tidy tables, and escape characters and raw strings.40 min
  1. Run it: what is really in a string
  2. Index and slice
  3. Search inside a string
  4. Strings never change
  5. Tidy columns with format specs
  6. Escapes and raw strings
Split and joinsplit() on a separator, on whitespace and with a limit, unpacking fields into names, reading a whole log file line by line, join() to glue text back together, and partition() for key=value telemetry.40 min
  1. Run it: split a line into fields
  2. Unpack into names
  3. Three ways to split
  4. Read the whole log
  5. Glue it back with join
  6. partition for key=value

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Split and join" 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: "partition for key=value".

Chapter 2. About 1 h 20 min

Regular expressions

Describe text instead of spelling it out, and pull out the pieces.

LessonTimeWhat students do
re basicsRegular expressions with re: search and the Match object, character classes and repeats, anchors and MULTILINE, counted repeats and word boundaries, re.split and re.sub, and compiled patterns with flags.40 min
  1. Run it: search for a pattern
  2. Character classes and repeats
  3. Anchors and MULTILINE
  4. Exact counts and word edges
  5. Split and replace with patterns
  6. Compile once, with flags
GroupsBrackets that capture: group() and groups(), turning a T-minus stamp into seconds, named groups and groupdict, findall with groups and non-capturing groups, back-references in re.sub, and finditer with positions.40 min
  1. Run it: brackets capture
  2. Turn a stamp into seconds
  3. Named groups
  4. findall with groups
  5. Back-references in re.sub
  6. Every match, with positions

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Groups" 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: "Every match, with positions".

Chapter 3. About 1 h 20 min

Parsing real feeds

A launch log and a ship's GPS, turned from text into data.

LessonTimeWhat students do
Parsing logsA parser for the whole launch log: one regex per line into (time, system, message) records, telling comments from garbage, counting by system, formatting stamps back, timing events by pattern, and pulling a time window.40 min
  1. Run it: a line in, a record out
  2. Comments are not errors
  3. Count by system
  4. Stamps back out
  5. Time between events
  6. A time window
Parsing NMEA linesA ship's GPS feed in NMEA 0183: the XOR checksum that catches corrupted sentences, GGA fields by position, degrees and minutes to decimal degrees, a regex that validates and sorts sentence types, and speed and course from RMC.40 min
  1. Run it: the ship talks
  2. Check the checksum
  3. GGA fields by position
  4. Degrees and minutes to decimal
  5. Validate with one pattern
  6. Speed and course

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Parsing NMEA lines" 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: "Speed and course".

Chapter 4. About 50 min

Project

A log analyzer that answers commands and lights an alarm.

LessonTimeWhat students do
Project: a log analyzerEverything in one tool: load and parse the launch log, then answer typed commands (grep with any regex, a time window, ranked stats), survive a broken pattern, and light a red alarm LED when the log holds warnings.50 min
  1. Run it: load the log
  2. A command prompt with grep
  3. Survive a broken pattern
  4. A time window command
  5. Light the alarm
  6. Ranked stats
  7. Make it yours

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Project: a log analyzer" 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: "Make it yours".

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.