CLI journal project (1/6): architecture and scope

A CLI that reads log files line by line. This chapter sets the parser, stats, and CLI modules, the line format, memory limits, and what done looks like.

Illustration : adolescent de dos codant sur un ordinateur portable le soir, carnet de croquis ouvert sur le bureau, pour l'architecture d'un projet de journal en Python

AI-generated illustration

Some links on this page are affiliate links. The button opens Amazon so you can check today’s price.

The journal-stats tool (the tutorial’s made-up name) reads one or more text files in a log-like format: one line per event, an optional timestamp prefix YYYY-MM-DD HH:MM:SS, and a level such as INFO or ERROR. The program produces statistics: line counts by level, the top messages, and errors by hour. The architecture splits parser (a generator of lines / records), stats (aggregation), cli (arguments, several files), and optionally io_async (a sketch of concurrent reading).

Project hub: advanced Python journal CLI.

Planned modules

  • journal_stats/parser.py: parse_lines(path) -> Iterator[LogLine]
  • journal_stats/stats.py: aggregate(lines: Iterable[LogLine]) -> Report
  • journal_stats/cli.py: main() with argparse
  • pyproject.toml: the entry point (part 6)

Exercises (12)

Exercise 1 — Define the minimum format of an accepted line (regex or split). Easy

Show the solution
2026-03-29 14:00:00 INFO free-form message

Exercise 2 — Why an Iterator rather than a list when reading a 1 GB file? Easy

Show the solution
# Constant memory; stream processing.

Exercise 3 — Name two risks if everything lives in cli.py. Intermediate

Show the solution
Hard to test; no reuse; layers mixed together.

Exercise 4 — TypedDict or dataclass for LogLine — the advantage of each. Intermediate

Show the solution
dataclass: methods and validation; TypedDict: a JSON-like dict.

Exercise 5 — A text UML sketch: cli → stats → parser. Easy

Show the solution
cli calls stats.aggregate(parser.parse(path))

Exercise 6 — A performance criterion: process 100 MB without using more than 500 MB of RAM — what strategy? Hard

Show the solution
Iterate line by line; integer counters; do not keep a list of every line.

Exercise 7 — Binary files are out of scope — how do you document that? Easy

Show the solution
README: UTF-8 text only; error if decoding fails.

Exercise 8 — The tool’s own logging versus analyzing logs — a mix-up to avoid. Intermediate

Show the solution
Use logging for debug; stdout for the user report.

Exercise 9 — Extension: filter by date range — where in the architecture? Hard

Show the solution
In the parser or a derived generator; stats already receives filtered lines.

Exercise 10 — An empty __init__.py is enough for a package — when do you use __all__? Intermediate

Show the solution
# from pkg import * — avoid it; __all__ limits what is exposed.

Exercise 11 — Compatibility with Python 3.10+: which typing feature should you use? Intermediate

Show the solution
X | None, list[str], ParamSpec if you need it.

Exercise 12 — Definition of Done for part 1. Easy

Show the solution
Tree created; line format documented; README with the goals.

Next

Parsing with generators

Amazon

Amazon.co.uk

Further reading

Was this page helpful?

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.