Project

Parse the Tower Logs

The lighthouse towers write logs like this:

2026-11-25 21:04:13 [INFO] tower-3: lantern lit
2026-11-25 21:05:40 [WARN] tower-12: fuel low (8%)

Long patterns get unreadable fast. re.VERBOSE lets you spread one over several lines with comments (spaces in the pattern are then ignored. Use \s or [ ] for a real space):

STAMP = re.compile(r'''
    (?P<date>\d{4}-\d{2}-\d{2})   # 2026-11-25
    \s+
    (?P<time>\d{2}:\d{2})         # 21:04
''', re.VERBOSE)

Assignment

  1. Complete LOG_RE with named groups stamp (date and time), level (capital letters), source (like tower-12) and message (the rest of the line). It's already compiled with re.VERBOSE.
  2. parse_line(line) returns None if the whole line doesn't match, otherwise a dict with keys "when" (a datetime), "level", "source" and "message".
  3. level_counts(lines) returns a Counter of levels for the lines that parse.
  4. fuel_alerts(lines) returns a list of (source, percent) tuples (with percent an int) for parsed lines whose message contains (N%).