Project 6 · Lecture 01 · 8 min
Project Description
Your task: turn the pull pipeline over cars.csv into a push pipeline of coroutines that filters cars by name and writes the matches to a CSV file.
The goal
In the pipelines lessons we built a pull pipeline over cars.csv: a generator parsed the file, a chain of filter generators kept only cars whose name contained certain words, and the caller iterated over the result. We also built a push pipeline that broadcast car data to several filters, each ending in a coroutine that saved its rows to a file.
Your job is to combine the two: rewrite the car-name pipeline in the push style. Rows from cars.csv should be pushed through any number of name filters and into a coroutine that saves the surviving rows to a CSV file.
The data
cars.csv has a header row and 406 data rows, separated by semicolons. The first column is the car’s name:
with open('cars.csv') as f:
for _ in range(4):
print(f.readline(), end='')Reading it with csv and a sniffed dialect gives lists of strings:
import csv
with open('cars.csv') as f:
dialect = csv.Sniffer().sniff(f.read(2000))
f.seek(0)
reader = csv.reader(f, dialect=dialect)
header = next(reader)
first = next(reader)
print('delimiter:', repr(dialect.delimiter))
print(header)
print(first)Requirements
- A data source. A generator that opens
cars.csv, detects the dialect, skips the header row and yields each data row (as a list of strings). It must close the file when it’s done or closed. - A sink. A coroutine that opens an output file and writes every row it receives with
csv.writer. You don’t need a header row in the output (add one if you like). The file must be complete once the coroutine is closed. - A filter. A generic coroutine that takes a predicate and a target, and forwards only the rows for which the predicate is true.
- A pipeline. A coroutine that, given an output file name and any number of name filters, builds the chain of filters ending in the sink, then forwards every row it receives into that chain.
- Automatic cleanup. A context manager that creates the pipeline and guarantees it is closed when the
withblock ends, even on errors.
Make the code as generic as you can: nothing in the filter or sink should know about cars.
Expected result
With the three filters 'Chevrolet', 'Carlo' and 'Landau', the output file must contain exactly two lines:
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
Note that csv.writer uses commas by default, so the output is comma-separated even though the input wasn’t.
Hints
- Use a priming decorator so every coroutine is ready for
send()as soon as it’s created. - Build the chain back to front: create the sink first, then wrap it in filters one at a time, each new filter targeting the previous one.
- If you create the filter predicates with
lambdainside a loop, remember how closures capture loop variables (Part 1, closures). All your lambdas could end up testing the last filter word. - A push pipeline only finishes when it’s closed. Make sure closing the pipeline coroutine reaches the sink, so its file gets flushed and closed.
Before reading the solution, open the Practice page for this section. Each requirement above is an auto-graded exercise with starter code and tests, from the data source all the way to the context-managed pipeline. Then compare your approach with the walkthrough in the next lesson.
Key takeaways
- The project turns a pull pipeline (generators iterating over each other) into a push pipeline (coroutines sending to targets).
- Pieces: a source generator, a CSV sink coroutine, a generic filter coroutine, a pipeline coroutine that chains any number of filters, and a context manager that closes it.
- Success check: filters
'Chevrolet','Carlo','Landau'produce exactly two output lines.