Skip to content
IT Vizag
← Advanced Python Features

▶ 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

Lesson 10 of 10 · 2 min read

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

🔒 Code example: sign up free (above) to unlock it.

The analyser

🔒 Code example: sign up free (above) to unlock it.

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

  1. Add a command-line argument for the file path (sys.argv or argparse).
  2. Count errors per hour using e.time[:13] as the key.
  3. Keep only the 5 slowest using heapq.nlargest instead of a growing list.
  4. 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.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend