Skip to Content
Functional Python Programming - Second Edition
book

Functional Python Programming - Second Edition

by Steven F. Lott
April 2018
Intermediate to advanced content levelIntermediate to advanced
408 pages
10h 42m
English
Packt Publishing
Content preview from Functional Python Programming - Second Edition

Filtering and reducing the raw data with a Counter object

We'll represent the essential defect counts as a collections.Counter parameter. We will build counts of defects by shift and defect type from the detailed raw data. Here's the code that reads some raw data from a .csv file:

from typing import TextIOimport csvfrom collections import Counterfrom types import SimpleNamespacedef defect_reduce(input_file: TextIO) -> Counter:    rdr = csv.DictReader(input_file)    assert set(rdr.fieldnames) == set(        ["defect_type", "serial_number", "shift"])    rows_ns = (SimpleNamespace(**row) for row in rdr)    defects = (        (row.shift, row.defect_type)        for row in rows_ns if row.defect_type)    tally = Counter(defects)    return tally

The preceding function will create a dictionary ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Python Programming - Third Edition

Functional Python Programming - Third Edition

Steven F. Lott

Publisher Resources

ISBN: 9781788627061Supplemental Content