Part 10 / Topic 11 of 11
Putting it together: log-processing pipeline
Real-world example: a web server writes one line per request into examples/sample_access.log:
2026-03-01T09:00:06 10.0.0.12 GET /api/products/42 200 373
(time, IP address, method, path, status code, response time in ms). We want a report of the busiest endpoints.
The program combines everything in this part:
- a generator pipeline:
read_lines→skip_noise→parse→only_status - a
@timerdecorator onbuild_report - a context manager
report_sectionthat prints the header and footer sorted(key=),max(key=),enumerate, a set comprehension
The heart of it (an excerpt: the other steps are in the full program):
def read_lines(path):
with path.open(encoding="utf-8") as file: # context manager closes the file
for line in file:
yield line.rstrip("\n") # one line in memory at a time
requests = list(parse(skip_noise(read_lines(LOG_FILE))))
Output (the timer value varies):
[timer] build_report took 0.3 ms
========================================
Access log report
========================================
Requests : 40
Avg time : 231 ms
Errors : 4 on /api/orders, /login
Slowest : /api/products (388 ms at 2026-03-01T09:02:27)
Top endpoints:
1. GET /api/products 13 hits
2. GET /api/products/42 8 hits
3. POST /api/orders 6 hits
========================================
Full program: examples/07_log_report.py.
Done here? The practice set for Part 10 — 8 exercises with solutions — is on the part page.