diff --git a/Exercises/ex2_2.md b/Exercises/ex2_2.md index 86e54ba..d041b70 100644 --- a/Exercises/ex2_2.md +++ b/Exercises/ex2_2.md @@ -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? diff --git a/Exercises/ex2_5.md b/Exercises/ex2_5.md index 00ccdbe..6525c96 100644 --- a/Exercises/ex2_5.md +++ b/Exercises/ex2_5.md @@ -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 = [] diff --git a/Exercises/ex5_1.md b/Exercises/ex5_1.md index e9707ba..cb08deb 100644 --- a/Exercises/ex5_1.md +++ b/Exercises/ex5_1.md @@ -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), diff --git a/Exercises/ex5_6.md b/Exercises/ex5_6.md index 8afe156..4f8c950 100644 --- a/Exercises/ex5_6.md +++ b/Exercises/ex5_6.md @@ -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) diff --git a/Exercises/ex6_2.md b/Exercises/ex6_2.md index a760bf5..cd90e83 100644 --- a/Exercises/ex6_2.md +++ b/Exercises/ex6_2.md @@ -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 ``` diff --git a/Exercises/soln5_3.md b/Exercises/soln5_3.md index 67ada42..217b1c2 100644 --- a/Exercises/soln5_3.md +++ b/Exercises/soln5_3.md @@ -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)) ``` diff --git a/Solutions/1_6/pcost.py b/Solutions/1_6/pcost.py index be53673..56518b8 100644 --- a/Solutions/1_6/pcost.py +++ b/Solutions/1_6/pcost.py @@ -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 diff --git a/Solutions/5_3/reader.py b/Solutions/5_3/reader.py index 1e68373..3926e7a 100644 --- a/Solutions/5_3/reader.py +++ b/Solutions/5_3/reader.py @@ -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): ''' diff --git a/Solutions/9_4/structly/tableformat/formatter.py b/Solutions/9_4/structly/tableformat/formatter.py index 893d7ba..2ce837e 100644 --- a/Solutions/9_4/structly/tableformat/formatter.py +++ b/Solutions/9_4/structly/tableformat/formatter.py @@ -1,4 +1,4 @@ -# tableformat.py +# formatter.py from abc import ABC, abstractmethod def print_table(records, fields, formatter):