Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Boiler Log Digest

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:

Language: Python
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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 Every Friday, Dev runs this Python tool on the week’s boiler log. It prints the peak, the count over the limit and each reading over it, then adds the week to the season file.

00:14 You’ve ported each part on its own. Now the whole tool moves to JavaScript, for the school’s new dashboard, and must print these four lines.

00:25 Each function in readings.js gets export, since a module shares only what it exports. isBlank() ports almost word for word: .strip() becomes .trim().

00:37 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.

00:51 Python’s summarise() builds two comprehensions and calls max() on one. JavaScript has no comprehensions; here, one for...of loop does both jobs.

01:03 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.

01:15 With no tuples, summarise() returns an array of the two.

01:21 The self-check can’t port as it is, since __name__ doesn’t exist in JavaScript at all. Instead, import.meta.main is true only in the file that node starts.

01:34 Run on its own, each file prints Wednesday’s reading, Python as a dict and JavaScript as an object.

01:43 Now for digest.js, the entry file. The import from readings gains ./ and .js: a bare name would look for a package.

01:54 For readFile(), the port imports node:fs/promises, one of Node’s own modules. There’s no main() function either: the entry file’s top level is the program.

02:09 process.argv starts with the node program and the script, so index 1 would be the script’s path. The port slices from 2, with no sys import: process is always there.

02:23 Destructuring unpacks Dev’s log name and limit, and Number() converts the limit as float() does, except that bad text gives NaN, not an error.

02:33 The usage check is left for the exercise.

02:38 The readings list carries over. readFile() reads the whole log in one call, with no with block. Without await, you’d get a promise, not the text.

02:50 It’s a string, not a file, and looping over a string walks its characters, in Python too. So the port splits it into lines, trimming off the final newline. The trim only cleans the ends, so the loop still skips blank lines and pushes each reading.

03:09 Destructuring unpacks summarise()’s pair. JavaScript’s numbers don’t split into int and float, so it prints 78 where the contractor’s report reads 78.0, and .toFixed(1) keeps the decimal.

03:24 Python’s len() becomes the .length property. The limit and each reading use .toFixed() too. The season file’s lines are the exercise’s too.

03:36 And the main guard isn’t needed: nothing imports the entry file.

03:42 Run both with Dev’s arguments. Python prints four lines. The port imports readings.js, self-check and all. How many lines will the port print?

03:55 Your turn: pause the video and predict.

04:03 Four, the same as Python’s.

04:07 Imported, readings.js has import.meta.main false, so its self-check stays quiet.

04:16 That’s the digest the JavaScript way: ./ and .js imports, arguments sliced from 2, an awaited read split into lines, and a loop for the comprehensions.

04:28 Next up is the exercise Finish the Boiler Log Digest. You’ll make the port tell Dev how to run it when the log name or the limit is missing, and add the week’s line to the season file.

Become a Member to join the conversation.