▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
Mini project: log file analyser
Stream a large log with generators and report the errors.
🎬 Video version coming soon — read it now
Let's build a tool that analyses a server log of any size with constant memory. It uses generators, comprehensions, Counter, dataclasses, type hints, a context manager and a decorator.
Create sample data
The analyser
Why it scales
The whole file is never in memory: read_lines yields one line, parse turns it into an Entry, and analyse updates counters and forgets it. Only the counters and the slow list grow. Try it with 2 million lines: the time goes up, the memory barely moves.
Challenges
- Add a command-line argument for the file path (
sys.argvor argparse). - Count errors per hour using
e.time[:13]as the key. - Keep only the 5 slowest using
heapq.nlargestinstead of a growing list. - Ask AI to refactor
analyse, then prove with asserts that the report is unchanged.
What you learned in this course
- Comprehensions, args/*kwargs, lambda, map and filter
- Iterators, generators and lazy pipelines
- Decorators and context managers
- Type hints with mypy, and collections and itertools
- Safe AI-assisted refactoring
Next course: Async & Performance. You'll measure where time goes and make Python fast.
In one line
You built a constant-memory log analyser from generators, counters and dataclasses, timed with your own decorator.
Share with a friend