MCPcopy Create free account
hub / github.com/dabeaz-course/practical-python / read_portfolio

Function read_portfolio

Solutions/3_2/report.py:4–23  ·  view source on GitHub ↗

Read a stock portfolio file into a list of dictionaries with keys name, shares, and price.

(filename)

Source from the content-addressed store, hash-verified

2import csv
3
4def read_portfolio(filename):
5 '''
6 Read a stock portfolio file into a list of dictionaries with keys
7 name, shares, and price.
8 '''
9 portfolio = []
10 with open(filename) as f:
11 rows = csv.reader(f)
12 headers = next(rows)
13
14 for row in rows:
15 record = dict(zip(headers, row))
16 stock = {
17 'name' : record['name'],
18 'shares' : int(record['shares']),
19 'price' : float(record['price'])
20 }
21 portfolio.append(stock)
22
23 return portfolio
24
25def read_prices(filename):
26 '''

Callers 1

portfolio_reportFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected