April 2018
Intermediate to advanced
408 pages
10h 42m
English
As an alternative to reading all of the raw data, we can look at processing only the summary counts. We want to create a Counter object similar to the previous example; this will have defect counts as a value with a key of shift and defect code. Given summaries, we simply create a Counter object from the input dictionary.
Here's a function that will read our summary data:
from typing import TextIOfrom collections import Counterimport csvdef defect_counts(source: TextIO) -> Counter: rdr = csv.DictReader(source) assert set(rdr.fieldnames) == set( ["defect_type", "serial_number", "shift"]) rows_ns = (SimpleNamespace(**row) for row in rdr) convert = map( lambda d: ((d.shift, d.defect_code), int(d.count)), rows_ns) return ...