python · level 3 · unit 7 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A tool you can is small pieces, each tested alone.
A real tool joins everything up. A generator streams the data, so a huge file never sits in memory. A dataclass gives each record a clear shape. argparse reads the options, and main catches expected errors and returns an exit code.
Before you ship it, test each piece on its own: the parser with good and bad lines, the counting with an empty input, main with a bad option. Small pure functions make that quick.
from collections import Counter
from dataclasses import dataclass
@dataclass(frozen=True)
class Visit:
page: str
seconds: int
def parse(lines):
for line in lines:
page, seconds = line.split(",")
yield Visit(page, int(seconds))
print(Counter(v.page for v in parse(["/home,12", "/shop,40", "/home,5"])).most_common()) Each step can be tested on its own, with a short list standing in for a real file.
guess first
Two generators in a row. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
def read(lines):
for line in lines:
print("read", line)
yield line
def numbers(lines):
for line in lines:
if line.isdigit():
yield int(line)
total = sum(numbers(read(["4", "x", "6"])))
print("total", total)
your turn
Your turn. Skip the broken rows.
A good row has exactly two parts. Fill the first blank with that number, and the second with the word that skips to the next row.
fix it
One log line has too many words. Fix it!
- The error says there are too many values to unpack. How many pieces does
splitmake here? - The message has spaces of its own, so
splitcuts it into extra pieces. - Use
line.split(" ", 2)to cut only at the first two spaces, then run.
nailed it
you shipped your first real tool. :D
- Generators stream the data. Dataclasses give it shape.
- Skip or report bad lines instead of crashing on them.
- Test each small piece before you ship the whole.
new words in your collection
- pipeline
- ship
This lesson isn’t saved. A free account remembers every lesson you finish.