Merge branch 'main' of https://github.com/dabeaz-course/python-mastery into main
This commit is contained in:
commit
75763875df
@ -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?
|
||||
|
@ -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 = []
|
||||
|
@ -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),
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
|
@ -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))
|
||||
```
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -10,11 +10,14 @@ def convert_csv(lines, converter, *, headers=None):
|
||||
|
||||
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) })
|
||||
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):
|
||||
'''
|
||||
|
@ -1,4 +1,4 @@
|
||||
# tableformat.py
|
||||
# formatter.py
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
def print_table(records, fields, formatter):
|
||||
|
Loading…
Reference in New Issue
Block a user