Porting the Race Results Cleaner
In this lesson, you ported the timing desk’s results cleaner to JavaScript, up to the lookup of finish times, and ran both programs on the same export to check the report’s first line.
- The Python script reads the chip-timing export, some rows with a stray space, a missing read, or a DNF: did not finish. It prints the race report: the bibs with a missing chip read, the average finish, and the fastest runner in each age group. The live results page is moving to JavaScript, so you rebuilt the script.
- The port starts with an import:
readFile()comes from the promise version of Node’s file system module. The folder’spackage.jsonsets the type to module, soimportworks, and so does a top-levelawait. to_seconds()becomes an arrow function with the same body, and Python’smap(int, ...)carries over asmap(Number)here. Notmap(parseInt):map()also passes each part’s index, andparseInt()reads that as a base.to_record()trims every cell, and so does the port: itsmap()callstrim()on each one. The cells unpack into three names and a rest element, Python’s starred target. A rest element can only come last, soslice(0, -1)takes the checkpoints andat(-1)the finish.- The return builds the record with
Number()forint()and shorthand for the rest:namealone meansname: name. Mei Chen’s row comes out with its stray space gone and her missing read kept as an empty string. - Python’s
withblock opens the export, and its loop reads line by line.readFile()opens, reads and closes the JavaScript folder’s copy in one call, as one string, andawaitwaits for it. Splitting the whole string leaves an empty last line, in Python too, sotrim()comes first, andsplit()gives seven lines.map()turns each line into a record, andtoRecord()ignores the index it also gets. - The finishers drop Lars Berg’s DNF and any row with no finish yet, as the live page runs from
the start gun, before anyone finishes.
&&, the JavaScriptand, joins the two tests. filter()keeps a finisher when not every checkpoint is there.every()does the work ofall(), but needs a test:all()checks truthiness. Thenmap()picks out the bibs, after the filter, while each runner still has its checkpoints. Three rows have a gap, butmissingholds only 23 and 58: the chain starts from the finishers, which left Lars Berg out.- Watch out: unlike Python’s empty list, an empty array counts as true, so
if (missing)would print the label alone at the start gun. The port asks for the length instead. The spread then hands each bib toconsole.log()as its own argument, as the star does in Python’s call. - The dict comprehension becomes
Object.fromEntries()over amap()of pairs, and the arrow’s parameter unpacks each record by name. The lookup’s keys are strings, as every object key is, where Python’s dict keeps numbers. A number bib still finds its entry, turned into the same string.
Here’s the desk’s Python script, results_cleaner.py:
def to_seconds(time):
h, m, s = map(int, time.split(":"))
return h * 3600 + m * 60 + s
def to_record(line):
cells = [cell.strip() for cell in line.split(",")]
bib, name, group, *splits = cells
*checkpoints, finish = splits
return {
"bib": int(bib),
"name": name,
"group": group,
"checkpoints": checkpoints,
"finish": finish,
}
with open("chip_reads.csv", encoding="utf-8") as f:
runners = [to_record(line) for line in f]
finishers = [
r for r in runners if r["finish"] not in ("", "DNF")
]
missing = [
r["bib"] for r in finishers if not all(r["checkpoints"])
]
if missing:
print("Missing chip reads:", *missing)
times = {r["bib"]: to_seconds(r["finish"]) for r in finishers}
average = sum(times.values()) / max(len(times), 1)
print(f"Average finish: {average / 60:.1f} min")
for group in sorted({r["group"] for r in runners}):
fastest = min(
(r for r in finishers if r["group"] == group),
key=lambda r: times[r["bib"]],
default=None,
)
if fastest is not None:
print(f"{group}: {fastest['name']} {fastest['finish']}")