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.
Getting started
Home Assistant
Buying
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.


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.
journal_stats/parser.py: parse_lines(path) -> Iterator[LogLine]journal_stats/stats.py: aggregate(lines: Iterable[LogLine]) -> Reportjournal_stats/cli.py: main() with argparsepyproject.toml: the entry point (part 6)Exercise 1 — Define the minimum format of an accepted line (regex or split). Easy
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
# Constant memory; stream processing.
Exercise 3 — Name two risks if everything lives in cli.py. Intermediate
Hard to test; no reuse; layers mixed together.
Exercise 4 — TypedDict or dataclass for LogLine — the advantage of each. Intermediate
dataclass: methods and validation; TypedDict: a JSON-like dict.
Exercise 5 — A text UML sketch: cli → stats → parser. Easy
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
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
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
Use logging for debug; stdout for the user report.
Exercise 9 — Extension: filter by date range — where in the architecture? Hard
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
# from pkg import * — avoid it; __all__ limits what is exposed.
Exercise 11 — Compatibility with Python 3.10+: which typing feature should you use? Intermediate
X | None, list[str], ParamSpec if you need it.
Exercise 12 — Definition of Done for part 1. Easy
Tree created; line format documented; README with the goals.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).