Using dataclasses to simplify working with CSV files
One commonly used data format is known as 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.
This leads to dictionary-based references to a column's data. We're forced to write, for example, row['lat']
or row['date']
to refer to data in specific columns. While this isn't horrible, it would be much nicer to use syntax like row.lat
or row.date
to refer to column values.
Additionally, we often have derived values that should – perhaps – be properties of a class definition instead of a separate function. This can properly encapsulate...