11.4 Using dataclasses to simplify working with CSV files
One commonly used data format is known as Comma-Separated Values (CSV). Python’s csv module has a very handy DictReader class definition. When a file contains a one-row header, the header row’s values become keys that are used for all the subsequent rows. This allows a great deal of flexibility in the logical layout of the data. For example, the column ordering doesn’t matter, since each column’s data is identified by a name taken from the header row.
Using a dictionary forces us to write, for example, row[’lat’] or row[’date’] to refer to data in specific columns. The built-in dict class has no provision for derived data. If we switch to a dataclass, we have a number of benefits:
Nicer attribute syntax like row.lat or row.date.
Derived values can be lazy properties.
A frozen dataclass is immutable, and the objects can...