Before the Lab
Read
~/cs2613/labs/L17
fileparse.py
containing a
function parse_csv
in Exercise 3.4 just above. Make sure that your
modified function passes the following test.def test_portfolio():
portfolio = parse_csv('Data/portfolio.csv', types=[str, int, float])
assert portfolio == [{'price': 32.2, 'name': 'AA', 'shares': 100},
{'price': 91.1, 'name': 'IBM', 'shares': 50},
{'price': 83.44, 'name': 'CAT', 'shares': 150},
{'price': 51.23, 'name': 'MSFT', 'shares': 200},
{'price': 40.37, 'name': 'GE', 'shares': 95},
{'price': 65.1, 'name': 'MSFT', 'shares': 50},
{'price': 70.44, 'name': 'IBM', 'shares': 100}]
def test_shares():
shares_held = parse_csv('Data/portfolio.csv', select=['name', 'shares'], types=[str, int])
assert shares_held == [{'name': 'AA', 'shares': 100}, {'name': 'IBM', 'shares': 50},
{'name': 'CAT', 'shares': 150}, {'name': 'MSFT', 'shares': 200},
{'name': 'GE', 'shares': 95}, {'name': 'MSFT', 'shares': 50},
{'name': 'IBM', 'shares': 100}]
types
?Complete Exercise
3.6. You only need to write a few lines of code, but you
will need to make several blocks of code conditional on
has_headers
. Be careful when re-indenting code, remember Python is
picky about indentation.
Make sure the following test (and your previous tests) pass
def test_tuple():
prices = parse_csv('Data/prices.csv', types=[str,float], has_headers=False)
assert prices == [('AA', 9.22), ('AXP', 24.85), ('BA', 44.85), ('BAC', 11.27),
('C', 3.72), ('CAT', 35.46), ('CVX', 66.67), ('DD', 28.47),
('DIS', 24.22), ('GE', 13.48), ('GM', 0.75), ('HD', 23.16),
('HPQ', 34.35), ('IBM', 106.28), ('INTC', 15.72), ('JNJ', 55.16),
('JPM', 36.9), ('KFT', 26.11), ('KO', 49.16), ('MCD', 58.99),
('MMM', 57.1), ('MRK', 27.58), ('MSFT', 20.89), ('PFE', 15.19),
('PG', 51.94), ('T', 24.79), ('UTX', 52.61), ('VZ', 29.26),
('WMT', 49.74), ('XOM', 69.35)]
The second half of the lab will be a quiz on JavaScript