An example reading a csv files into a dynamically generated dataclass.
|
|
||
|---|---|---|
| main.py | More elegant inspection. | |
| README.md | Adding a README.md. | |
| testdata.csv | Some test data as given | |
csv_reader
We have a file testdata.csv:
sep=,
field_a,field_b,field_c
10.0,20.0,30.0
1.0,2.0,3.0
We want to read this file into a frozen dataclass with slots, but the dataclass should have field names from the csv file.
This can be done using make_dataclass.
C = make_dataclass('MyClass',
[('field_a', float),
('field_b', float),
('field_c', float),]
is the same as
@dataclass
class C:
field_a: float
field_b: float
field_c: float
The code shows how to do this, sanitizing field names, and typing the dataclass.