Added solution code
This commit is contained in:
32
Solutions/2_16/pcost.py
Normal file
32
Solutions/2_16/pcost.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# pcost.py
|
||||
|
||||
import csv
|
||||
def portfolio_cost(filename):
|
||||
'''
|
||||
Computes the total cost (shares*price) of a portfolio file
|
||||
'''
|
||||
total_cost = 0.0
|
||||
|
||||
with open(filename) as f:
|
||||
rows = csv.reader(f)
|
||||
headers = next(rows)
|
||||
for rowno, row in enumerate(rows, start=1):
|
||||
record = dict(zip(headers, row))
|
||||
try:
|
||||
nshares = int(record['shares'])
|
||||
price = float(record['price'])
|
||||
total_cost += nshares * price
|
||||
# This catches errors in int() and float() conversions above
|
||||
except ValueError:
|
||||
print(f'Row {rowno}: Bad row: {row}')
|
||||
|
||||
return total_cost
|
||||
|
||||
import sys
|
||||
if len(sys.argv) == 2:
|
||||
filename = sys.argv[1]
|
||||
else:
|
||||
filename = input('Enter a filename:')
|
||||
|
||||
cost = portfolio_cost(filename)
|
||||
print('Total cost:', cost)
|
||||
67
Solutions/2_16/report.py
Normal file
67
Solutions/2_16/report.py
Normal file
@@ -0,0 +1,67 @@
|
||||
# report.py
|
||||
import csv
|
||||
|
||||
def read_portfolio(filename):
|
||||
'''
|
||||
Read a stock portfolio file into a list of dictionaries with keys
|
||||
name, shares, and price.
|
||||
'''
|
||||
portfolio = []
|
||||
with open(filename) as f:
|
||||
rows = csv.reader(f)
|
||||
headers = next(rows)
|
||||
|
||||
for row in rows:
|
||||
record = dict(zip(headers, row))
|
||||
stock = {
|
||||
'name' : record['name'],
|
||||
'shares' : int(record['shares']),
|
||||
'price' : float(record['price'])
|
||||
}
|
||||
portfolio.append(stock)
|
||||
|
||||
return portfolio
|
||||
|
||||
def read_prices(filename):
|
||||
'''
|
||||
Read a CSV file of price data into a dict mapping names to prices.
|
||||
'''
|
||||
prices = {}
|
||||
with open(filename) as f:
|
||||
rows = csv.reader(f)
|
||||
for row in rows:
|
||||
try:
|
||||
prices[row[0]] = float(row[1])
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
return prices
|
||||
|
||||
def make_report_data(portfolio, prices):
|
||||
'''
|
||||
Make a list of (name, shares, price, change) tuples given a portfolio list
|
||||
and prices dictionary.
|
||||
'''
|
||||
rows = []
|
||||
for stock in portfolio:
|
||||
current_price = prices[stock['name']]
|
||||
change = current_price - stock['price']
|
||||
summary = (stock['name'], stock['shares'], current_price, change)
|
||||
rows.append(summary)
|
||||
return rows
|
||||
|
||||
# Read data files and create the report data
|
||||
|
||||
portfolio = read_portfolio('../../Work/Data/portfolio.csv')
|
||||
prices = read_prices('../../Work/Data/prices.csv')
|
||||
|
||||
# Generate the report data
|
||||
|
||||
report = make_report_data(portfolio, prices)
|
||||
|
||||
# Output the report
|
||||
headers = ('Name', 'Shares', 'Price', 'Change')
|
||||
print('%10s %10s %10s %10s' % headers)
|
||||
print(('-' * 10 + ' ') * len(headers))
|
||||
for row in report:
|
||||
print('%10s %10d %10.2f %10.2f' % row)
|
||||
Reference in New Issue
Block a user