Project 17 of 20, for Neil
Log Streamer
Search Neil's sim-racing telemetry, too long to read all at once: a chain of generators that reads a line at a time and stops as soon as it has enough.
Sign up free to open itLog in
Act 2 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilMy racing sim logs every part of the car a line a minute for every race, and it's years long now.
- NeilMy old report reads the whole lot into a list first, and this morning it took fifteen minutes.
- NeilI only want the first few that matter, which are the brake errors from last weekend's 24-hour race.
- PaulThen don't read what you don't need, just hand it over a line at a time.
race.log has a line a minute for each part of the car, time | level | system | message, and the odd line that doesn't. Write logstream.py as a chain of generators, so it never reads further than it has to:
read_lines(path)yields each line of the file without its newline, skipping blank lines.parse(lines)yields(time, level, system, message)for each line with all four parts, each part stripped and the level in capitals. Split withsplit("|", 3), so a message with|in it stays whole. Lines without four parts are skipped.matching(entries, level=None, system=None)yields only the entries at that level (whatever its capitals) and from that system.Nonemeans any.first(entries, count)returns a list of the firstcountentries, taking no more than that fromentries.
main takes --level, --system and --first (a number, 10 if it's not given), chains the four together and prints each entry as 06:12 ERROR brakes: overheating, or Nothing found.
$ python logstream.py --level ERROR --system brakes --first 3 06:12 ERROR brakes: overheating 06:37 ERROR brakes: overheating 07:57 ERROR brakes: overheating
What you learn
What's in it
- One at a time
- One at a time
- A function that pauses
- Paused at the yield
- Only what's asked for
- Who does the work, and when
- How Paul would do it
- Fix Yahya's version
- Make the log streamer
- Ship it