Initial commit
This commit is contained in:
55
Exercises/soln5_1.md
Normal file
55
Exercises/soln5_1.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# Exercise 5.1 - Solution
|
||||
|
||||
This is a partial solution that does not include type-hints.
|
||||
|
||||
```python
|
||||
# reader.py
|
||||
|
||||
import csv
|
||||
|
||||
def csv_as_dicts(lines, types, *, headers=None):
|
||||
'''
|
||||
Convert lines of CSV data into a list of dictionaries
|
||||
'''
|
||||
records = []
|
||||
rows = csv.reader(lines)
|
||||
if headers is None:
|
||||
headers = next(rows)
|
||||
for row in rows:
|
||||
record = { name: func(val)
|
||||
for name, func, val in zip(headers, types, row) }
|
||||
records.append(record)
|
||||
return records
|
||||
|
||||
def csv_as_instances(lines, cls, *, headers=None):
|
||||
'''
|
||||
Convert lines of CSV data into a list of instances
|
||||
'''
|
||||
records = []
|
||||
rows = csv.reader(lines)
|
||||
if headers is None:
|
||||
headers = next(rows)
|
||||
for row in rows:
|
||||
record = cls.from_row(row)
|
||||
records.append(record)
|
||||
return records
|
||||
|
||||
def read_csv_as_dicts(filename, types, *, headers=None):
|
||||
'''
|
||||
Read CSV data into a list of dictionaries with optional type conversion
|
||||
'''
|
||||
with open(filename) as file:
|
||||
return csv_as_dicts(file, types, headers=headers)
|
||||
|
||||
def read_csv_as_instances(filename, cls, *, headers=None):
|
||||
'''
|
||||
Read CSV data into a list of instances
|
||||
'''
|
||||
with open(filename) as file:
|
||||
return csv_as_instances(file, cls, headers=headers)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
[Back](ex5_1.md)
|
||||
Reference in New Issue
Block a user