Streams of structured data

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Streams of structured data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes a stream of records?
A Records that arrive or are read one at a time and processed as they flow
B All records loaded into memory at once before any processing starts
2. A temperature sensor sends a new reading every second. Is this a stream?
A Yes, each reading arrives one at a time as a structured record
B No, sensor readings can only be processed after the sensor stops
3. A program reads a CSV file by looping over its lines. Each line is treated as:
A One record in the stream
B The entire stream at once
4. Which is an example of a stream source?
A A single integer variable called total
B Results returned by an API, read one record at a time
5. In a stream, when is each record processed?
A Only after every record has been collected into one big array
B As it is read, one record at a time

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for line in open("readings.csv"): AReads a file as a stream, giving the program one line (one record) at a time
2for reading in sensor_feed: BThe loop replaces total with each reading instead of adding to it, so nothing builds up as the stream flows. The code should use total = total + reading, after setting total to 0 before the loop.
3A stream is processed one record at a time, so a loop handles each record in turn CProcesses sensor readings one record at a time as they arrive
4API results can be treated as a stream of records, with each result processed as it is read Dread() loads the whole file into one string, so the loop gets single characters instead of lines. It no longer treats the file as a stream of records.
5data = open("readings.csv").read() for line in data: Efor record in stream: print(record)
6for reading in sensor_feed: total = reading print(total) Ffor result in api_results: process(result)

Definitions

stream

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___ reading in sensor_stream: print(reading)
2.
for line in ____("log.csv"): print(line)
3.
for record __ api_results: print(record)
4.
total = 0 for r in readings: total = total _ r["temp"]
5.
for line in open("data.csv"): fields = line._____(",") print(fields[0])

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A file = open("temps.csv")
B for line in file
C parts = line.split(",")
D print(parts[0])
Question 2
A total = 0
B for r in range(readings):
C total = total + r
D print(total)
Question 3
A for line in open("log.csv"):
B fields = line.split(",")
C temp = float(fields(1))
D print(temp)
Question 4
A with open(data.csv) as f:
B for line in f:
C print(line.strip())
Question 5
A results = get_results(url)
B for item in results:
C name = item["name"]
D print(Name)

Answer Key

Streams of structured data
Section A: Quick Questions
Q1
A — Records that arrive or are read one at a time and processed as they flow
Q2
A — Yes, each reading arrives one at a time as a structured record
Q3
A — One record in the stream
Q4
B — Results returned by an API, read one record at a time
Q5
B — As it is read, one record at a time
Section B: Match the Pairs
Q1
for line in open("readings.csv"): → Reads a file as a stream, giving the program one line (one record) at a time
Q2
for reading in sensor_feed: → Processes sensor readings one record at a time as they arrive
Q3
A stream is processed one record at a time, so a loop handles each record in turn → for record in stream: print(record)
Q4
API results can be treated as a stream of records, with each result processed as it is read → for result in api_results: process(result)
Q5
data = open("readings.csv").read() for line in data: → read() loads the whole file into one string, so the loop gets single characters instead of lines. It no longer treats the file as a stream of records.
Q6
for reading in sensor_feed: total = reading print(total) → The loop replaces total with each reading instead of adding to it, so nothing builds up as the stream flows. The code should use total = total + reading, after setting total to 0 before the loop.
Section C: Definitions
Q1
stream — A sequence of structured records that arrives or is read one at a time, such as sensor readings, file lines or API results, and is processed as it flows.
Section D: Fill in the Blanks
Q1
for reading in sensor_stream: print(reading)
Q2
for line in open("log.csv"): print(line)
Q3
for record in api_results: print(record)
Q4
total = 0 for r in readings: total = total + r["temp"]
Q5
for line in open("data.csv"): fields = line.split(",") print(fields[0])
Section E: Spot the Error
Q1
B — A for loop header must end with a colon. It should be: for line in file:
Q2
B — range() needs a number, not a list. To read each sensor reading one at a time, loop directly: for r in readings:
Q3
C — A list item is picked with square brackets, not round ones. It should be: temp = float(fields[1])
Q4
A — The file name is text, so it needs quotes. Without them Python looks for variables called data and csv. It should be: with open("data.csv") as f:
Q5
D — Variable names are case sensitive. The variable was created as name, so Name does not exist. It should be: print(name)