Project 6 · Practice
Quiz & exercises
Answer each question to see the explanation. Your best score is saved in this browser.
Quiz
Coding exercises
Write your code, then press Check (or Shift+Enter) to run the tests. Drafts are saved as you type.
Exercise 1: The data source ✓ Passed
Write a generator parse_data(f_name) that opens the CSV file f_name, detects its dialect with csv.Sniffer (from the first 2000 characters), skips the header row, and yields each data row as a list of strings.
It must close the file when it finishes or is closed early. cars.csv (semicolon-separated, 406 data rows) is available in the working directory.
import csv
def parse_data(f_name):
with open(f_name) as f:
yield from csv.reader(f)
import itertools
def test_first_row():
"""the first row is the first car, split into fields"""
row = next(parse_data('cars.csv'))
assert row[0] == 'Chevrolet Chevelle Malibu', f"first row starts with {row[0]!r}"
assert len(row) == 9, f"expected 9 fields, got {len(row)}"
def test_row_count():
"""yields all 406 data rows, without the header"""
rows = list(parse_data('cars.csv'))
assert len(rows) == 406, f"got {len(rows)} rows"
assert rows[0][0] != 'Car', "the header row was not skipped"
def test_lazy():
"""it is a lazy generator"""
import types
assert isinstance(parse_data('cars.csv'), types.GeneratorType)
def test_fields_are_strings():
"""fields are strings"""
row = next(itertools.islice(parse_data('cars.csv'), 5, None))
assert all(isinstance(v, str) for v in row)
Hints (2)
csv.Sniffer().sniff(f.read(2000))returns a dialect; callf.seek(0)afterwards to go back to the start.- Skip the header with
next(f)(ornext(reader)), thenyield fromthe reader.
Show a solution
import csv
def parse_data(f_name):
f = open(f_name)
try:
dialect = csv.Sniffer().sniff(f.read(2000))
f.seek(0)
next(f) # skip the header row
yield from csv.reader(f, dialect=dialect)
finally:
f.close()Exercise 2: The CSV sink ✓ Passed
Write an auto-primed coroutine save_csv(f_name) (a coroutine priming decorator is provided). It opens f_name for writing, and writes each row it receives with csv.writer. When the coroutine is closed, the file must be closed, so all rows are on disk.
Open the file with newline='', as the csv module recommends.
import csv
def coroutine(fn):
def inner(*args, **kwargs):
g = fn(*args, **kwargs)
next(g)
return g
return inner
def save_csv(f_name):
f = open(f_name, 'w', newline='')
writer = csv.writer(f)
while True:
row = yield
writer.writerow(row)
from inspect import getgeneratorstate
def test_primed():
"""save_csv returns a primed coroutine"""
s = save_csv('ex_out.csv')
assert getgeneratorstate(s) == 'GEN_SUSPENDED', "the coroutine was not primed"
s.close()
def test_writes_rows():
"""rows sent in are written as CSV lines"""
s = save_csv('ex_out.csv')
s.send(['Ford Torino', '17.0', '8'])
s.send(['AMC Hornet', '18.0', '6'])
s.close()
with open('ex_out.csv') as f:
lines = f.read().splitlines()
assert lines == ['Ford Torino,17.0,8', 'AMC Hornet,18.0,6'], f"got {lines}"
def test_file_closed_on_close():
"""closing the coroutine closes the file"""
s = save_csv('ex_out.csv')
s.send(['x'])
s.close()
with open('ex_out.csv') as f:
assert f.read().strip() == 'x', "the row was not flushed to disk"
Hints (2)
- Decorate the function with
@coroutineso it's primed when created. - Open the file with
with:close()raisesGeneratorExitat theyield, which exits thewithblock and closes the file.
Show a solution
import csv
def coroutine(fn):
def inner(*args, **kwargs):
g = fn(*args, **kwargs)
next(g)
return g
return inner
@coroutine
def save_csv(f_name):
with open(f_name, 'w', newline='') as f:
writer = csv.writer(f)
while True:
row = yield
writer.writerow(row)Exercise 3: Chain any number of name filters ✓ Passed
filter_data(pred, target) should forward a row to target only if pred(row) is true, and close target when it is closed itself.
build_chain(target, words) should return the head of a chain of filter_data coroutines, one per word, ending in target. A row reaches target only if every word appears in its first field (row[0]).
The starter's build_chain has the classic closure-in-a-loop bug, and filter_data doesn't pass on close(). Fix both.
def coroutine(fn):
def inner(*args, **kwargs):
g = fn(*args, **kwargs)
next(g)
return g
return inner
@coroutine
def filter_data(pred, target):
while True:
row = yield
if pred(row):
target.send(row)
def build_chain(target, words):
for word in words:
target = filter_data(lambda row: word in row[0], target)
return target
from inspect import getgeneratorstate
@coroutine
def collect(results):
while True:
results.append((yield))
def run(words, names):
out = []
head = build_chain(collect(out), words)
for name in names:
head.send([name, '1'])
return [row[0] for row in out]
NAMES = ['Chevrolet Monte Carlo Landau', 'Ford LTD Landau',
'Chevrolet Monte Carlo', 'Chevrolet Impala']
def test_single_word():
"""one word filters on the first field"""
assert run(['Impala'], NAMES) == ['Chevrolet Impala']
def test_all_words_must_match():
"""every word must appear (no closure bug)"""
got = run(['Chevrolet', 'Carlo', 'Landau'], NAMES)
assert got == ['Chevrolet Monte Carlo Landau'], f"got {got}"
def test_no_words():
"""with no words every row passes"""
assert run([], NAMES) == NAMES
def test_close_reaches_sink():
"""closing the head closes the target"""
sink = collect([])
head = build_chain(sink, ['a', 'b'])
head.close()
assert getgeneratorstate(sink) == 'GEN_CLOSED', "the target was not closed"
Hints (2)
- All the lambdas share the single loop variable
word. Freeze its current value with a default argument:lambda row, word=word: .... - Wrap
filter_data's loop intry/finallyand calltarget.close()in thefinally.
Show a solution
def coroutine(fn):
def inner(*args, **kwargs):
g = fn(*args, **kwargs)
next(g)
return g
return inner
@coroutine
def filter_data(pred, target):
try:
while True:
row = yield
if pred(row):
target.send(row)
finally:
target.close()
def build_chain(target, words):
for word in words:
target = filter_data(lambda row, word=word: word in row[0], target)
return targetExercise 4: The context-managed pipeline ✓ Passed
Put it all together. parse_data, coroutine, save_csv and filter_data are provided (as in the earlier exercises). Write:
- pipeline_coro(out_file, name_filters): an auto-primed coroutine that builds a chain of name filters ending in save_csv(out_file), forwards every row it receives to the head of that chain, and closes the chain when it is closed; - pipeline(out_file, name_filters): a context manager that creates the pipeline coroutine, gives it to the with block, and always closes it afterwards.
With the filters ('Chevrolet', 'Carlo', 'Landau') the output file must contain exactly the two Chevrolet Monte Carlo Landau rows, comma-separated.
import csv
from contextlib import contextmanager
def parse_data(f_name):
f = open(f_name)
try:
dialect = csv.Sniffer().sniff(f.read(2000))
f.seek(0)
next(f)
yield from csv.reader(f, dialect=dialect)
finally:
f.close()
def coroutine(fn):
def inner(*args, **kwargs):
g = fn(*args, **kwargs)
next(g)
return g
return inner
@coroutine
def save_csv(f_name):
with open(f_name, 'w', newline='') as f:
writer = csv.writer(f)
while True:
writer.writerow((yield))
@coroutine
def filter_data(pred, target):
try:
while True:
row = yield
if pred(row):
target.send(row)
finally:
target.close()
@coroutine
def pipeline_coro(out_file, name_filters):
# build the chain, then forward rows to its head
while True:
row = yield
@contextmanager
def pipeline(out_file, name_filters):
yield pipeline_coro(out_file, name_filters)
def read_lines(name):
with open(name) as f:
return f.read().splitlines()
def feed(out_file, words):
with pipeline(out_file, words) as p:
for row in parse_data('cars.csv'):
p.send(row)
def test_landau():
"""Chevrolet + Carlo + Landau gives the two expected rows"""
feed('p_out.csv', ('Chevrolet', 'Carlo', 'Landau'))
assert read_lines('p_out.csv') == [
'Chevrolet Monte Carlo Landau,15.5,8,350.0,170.0,4165.,11.4,77,US',
'Chevrolet Monte Carlo Landau,19.2,8,305.0,145.0,3425.,13.2,78,US',
], f"got {read_lines('p_out.csv')}"
def test_filter_order_irrelevant():
"""the order of the filter words doesn't matter"""
feed('p_out.csv', ('Landau', 'Chevrolet', 'Carlo'))
assert len(read_lines('p_out.csv')) == 2
def test_single_filter():
"""a single filter keeps all 46 Chevrolets"""
feed('p_out.csv', ('Chevrolet',))
lines = read_lines('p_out.csv')
assert len(lines) == 46, f"got {len(lines)} rows"
assert all(line.startswith('Chevrolet') for line in lines)
def test_closed_on_error():
"""rows sent before an exception are still saved"""
try:
with pipeline('p_out.csv', ('Ford',)) as p:
p.send(['Ford Pinto', '25.0'])
p.send(['AMC Gremlin', '21.0'])
raise RuntimeError('boom')
except RuntimeError:
pass
assert read_lines('p_out.csv') == ['Ford Pinto,25.0'], f"got {read_lines('p_out.csv')}"
Hints (3)
- Start with
head = save_csv(out_file), then for each word wrap it:head = filter_data(<predicate>, head). Remember the default-argument trick for the lambda. - In
pipeline_coro, put the forwarding loop intry/finallyand closeheadin thefinally. - In
pipeline, create the coroutine,yieldit insidetry, and close it infinally.
Show a solution
import csv
from contextlib import contextmanager
def parse_data(f_name):
f = open(f_name)
try:
dialect = csv.Sniffer().sniff(f.read(2000))
f.seek(0)
next(f)
yield from csv.reader(f, dialect=dialect)
finally:
f.close()
def coroutine(fn):
def inner(*args, **kwargs):
g = fn(*args, **kwargs)
next(g)
return g
return inner
@coroutine
def save_csv(f_name):
with open(f_name, 'w', newline='') as f:
writer = csv.writer(f)
while True:
writer.writerow((yield))
@coroutine
def filter_data(pred, target):
try:
while True:
row = yield
if pred(row):
target.send(row)
finally:
target.close()
@coroutine
def pipeline_coro(out_file, name_filters):
head = save_csv(out_file)
for word in name_filters:
head = filter_data(lambda row, word=word: word in row[0], head)
try:
while True:
row = yield
head.send(row)
finally:
head.close()
@contextmanager
def pipeline(out_file, name_filters):
p = pipeline_coro(out_file, name_filters)
try:
yield p
finally:
p.close()