This commit is contained in:
David Beazley 2024-07-31 11:11:16 -05:00
commit 75763875df
9 changed files with 15 additions and 14 deletions

View File

@ -152,7 +152,7 @@ The key feature that makes this work is that a defaultdict
automatically initializes elements for you--allowing an insertion of a
new element and an `append()` operation to be combined together.
## (c) Data Analysis Challenge
## (d) Data Analysis Challenge
In the last exercise you just wrote some code to read CSV-data related
to the Chicago Transit Authority. For example, you can grab the data
@ -167,7 +167,7 @@ as dictionaries like this:
It would be a shame to do all of that work and then do nothing with
the data.
In this exercise, you task is this: write a program to answer the
In this exercise, your task is this: write a program to answer the
following three questions:
1. How many bus routes exist in Chicago?

View File

@ -60,7 +60,6 @@ and adding a few more values to it:
```python
>>> row = { 'route': '22', 'date': '01/01/2001', 'daytype': 'U', 'rides': 7354 }
>>> sys.getsizeof(row)
>>> sys.getsizeof(row)
240
>>> row['a'] = 1
>>> sys.getsizeof(row)
@ -206,7 +205,7 @@ into 4 separate `append()` operations.
class RideData(collections.abc.Sequence):
def __init__(self):
# Each value is a list with all of the values (a column)
# Each value is a list with all the values (a column)
self.routes = []
self.dates = []
self.daytypes = []

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

@ -25,7 +25,7 @@ import stock
class TestStock(unittest.TestCase):
def test_create(self):
s = Stock('GOOG', 100, 490.1)
s = stock.Stock('GOOG', 100, 490.1)
self.assertEqual(s.name, 'GOOG')
self.assertEqual(s.shares, 100)
self.assertEqual(s.price, 490.1)

View File

@ -160,7 +160,7 @@ class Stock(Structure):
def cost(self):
return self.shares * self.price
def sell(self, nshares):
def sell(self, shares):
self.shares -= shares
```

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

@ -12,7 +12,7 @@ def portfolio_cost(filename):
# This catches errors in int() and float() conversions above
except ValueError as e:
print("Couldn't parse:", line)
print("Couldn't parse:", repr(line))
print("Reason:", e)
return total_cost

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):
'''

View File

@ -1,4 +1,4 @@
# tableformat.py
# formatter.py
from abc import ABC, abstractmethod
def print_table(records, fields, formatter):