
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
- Complete
LOG_REwith named groupsstamp(date and time),level(capital letters),source(liketower-12) andmessage(the rest of the line). It's already compiled withre.VERBOSE. parse_line(line)returnsNoneif the whole line doesn't match, otherwise a dict with keys"when"(adatetime),"level","source"and"message".level_counts(lines)returns aCounterof levels for the lines that parse.fuel_alerts(lines)returns a list of(source, percent)tuples (withpercentanint) for parsed lines whose message contains(N%).
- Complete
LOG_REwith named groupsstamp(date and time),level(capital letters),source(liketower-12) andmessage(the rest of the line). It's already compiled withre.VERBOSE. parse_line(line)returnsNoneif the whole line doesn't match, otherwise a dict with keys"when"(adatetime),"level","source"and"message".level_counts(lines)returns aCounterof levels for the lines that parse.fuel_alerts(lines)returns a list of(source, percent)tuples (withpercentanint) for parsed lines whose message contains(N%).
main.py Python sleeping
Press Run to try your code, then Check to test it.