Porting the Boiler Log Digest
You’ve ported each part of Dev’s tool on its own. In this lesson, the whole tool moved to JavaScript, for the school’s new dashboard. Dev’s Python tool is two files: readings.py parses the log, and digest.py is the script he runs every Friday. Here’s readings.py:
def is_blank(line):
return line.strip() == ""
def parse_line(line):
time, temp = line.strip().split(",")
return {"time": time, "temp": float(temp)}
def summarise(readings, threshold):
temps = [r["temp"] for r in readings]
over = [r for r in readings if r["temp"] > threshold]
return max(temps), over
if __name__ == "__main__":
print(parse_line("Wed 07:00,76.5"))
Each function in readings.js gets export, since a module shares only what it exports. isBlank() ports almost word for word: .strip() becomes .trim(). In parseLine(), the unpacking becomes array destructuring. The dict becomes an object, where time on its own means time: time, and Number() does float()’s job.
Python’s summarise() builds two comprehensions and calls max() on one. JavaScript has no comprehensions; here, one for...of loop does both jobs. Math.max() compares numbers, not a list, so the loop keeps the highest so far, starting from -Infinity. Readings over the limit get pushed onto an array, and with no tuples, summarise() returns an array of the two.