Merge pull request #41 from juliomorero/fix-ex5

Fix 5_1 and 5_3
This commit is contained in:
David Beazley 2024-07-31 10:43:11 -05:00 committed by GitHub
commit 8990494ab4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 6 deletions

View File

@ -115,7 +115,7 @@ kinds of input sources. For example:
```python
>>> import gzip
>>> import stock
>>> file = gzip.open('Data/portfolio.csv.gz')
>>> file = gzip.open('Data/portfolio.csv.gz', 'rt')
>>> port = reader.csv_as_instances(file, stock.Stock)
>>> port
[Stock('AA', 100, 32.2), Stock('IBM', 50, 91.1), Stock('CAT', 150, 83.44),

View File

@ -48,11 +48,10 @@ def read_csv_as_instances(filename, cls, *, headers=None):
import csv
def convert_csv(lines, converter, *, headers=None):
records = []
rows = csv.reader(lines)
if headers is None:
headers = next(rows)
return map(lambda row: converter(headers, row), rows)
return list(map(lambda row: converter(headers, row), rows))
```

View File

@ -9,12 +9,15 @@ def convert_csv(lines, converter, *, headers=None):
return list(map(lambda row: converter(headers, row), rows))
def csv_as_dicts(lines, types, *, headers=None):
return convert_csv(lines,
lambda headers, row: { name: func(val) for name, func, val in zip(headers, types, row) })
return convert_csv(lines,
lambda headers, row: {name: func(val) for name, func, val in zip(headers, types, row)},
headers=headers)
def csv_as_instances(lines, cls, *, headers=None):
return convert_csv(lines,
lambda headers, row: cls.from_row(row))
lambda headers, row: cls.from_row(row),
headers=headers)
def read_csv_as_dicts(filename, types, *, headers=None):
'''