|
| 1 | +# pcost.py |
| 2 | + |
| 3 | +import csv |
| 4 | +import gzip |
| 5 | +from fileparse01 import parse_csv |
| 6 | + |
| 7 | +#def read_portfolio(filename): |
| 8 | +# '''Compute the total cost (shares*price) of a portfolio file''' |
| 9 | +# portfolio = [] |
| 10 | +# holding = {} |
| 11 | +# |
| 12 | +# with open(filename, 'rt') as f: |
| 13 | +# rows = csv.reader(f) |
| 14 | +# headers = next(rows) |
| 15 | +# for rowno, row in enumerate(rows, start=1): |
| 16 | +# record = dict(zip(headers, row)) |
| 17 | +# try: |
| 18 | +# holding = { |
| 19 | +# 'name': record['name'], |
| 20 | +# 'shares': int(record['shares']), |
| 21 | +# 'price': float(record['price']) |
| 22 | +# } |
| 23 | +# portfolio.append(holding) |
| 24 | +# except ValueError: |
| 25 | +# print(f'Row {rowno}: Bad row: {row}') |
| 26 | +# |
| 27 | +# return portfolio |
| 28 | +# |
| 29 | +# |
| 30 | +#def read_prices(filename): |
| 31 | +# |
| 32 | +# out = {} |
| 33 | +# f=open(filename, 'r') |
| 34 | +# rows = csv.reader(f) |
| 35 | +# for row in rows: |
| 36 | +# if len(row)>1 : |
| 37 | +# out[row[0]] = float(row[1]) |
| 38 | +# |
| 39 | +# return out |
| 40 | + |
| 41 | +def make_report(ports, price): |
| 42 | + sval = 0 |
| 43 | + cval = 0 |
| 44 | + curval = 0 |
| 45 | + report = [] |
| 46 | + |
| 47 | +# ports = read_portfolio(folioname) |
| 48 | +# price = read_prices(pricename) |
| 49 | + for port in ports: |
| 50 | + stkval = port['shares'] * port['price'] |
| 51 | + sval += stkval |
| 52 | + |
| 53 | + skey = port['name'] |
| 54 | + rid = 0 |
| 55 | + for idx, key in enumerate(price): |
| 56 | + if key[0] == skey: |
| 57 | + rid == idx |
| 58 | + break |
| 59 | + |
| 60 | + curval = price[idx][1] * port['shares'] |
| 61 | + cval += curval |
| 62 | + # **using tuple e |
| 63 | + lineval = ( |
| 64 | + port['name'], |
| 65 | + port['shares'], |
| 66 | + port['price'], |
| 67 | + float(curval - stkval) |
| 68 | + ) |
| 69 | + |
| 70 | + # **using dict |
| 71 | + # lineval = { |
| 72 | + # 'name': port['name'], |
| 73 | + # 'shares': port['shares'], |
| 74 | + # 'price': port['price'], |
| 75 | + # 'change': float(curval - stkval) |
| 76 | + # } |
| 77 | + report.append(lineval) |
| 78 | + |
| 79 | + return report |
| 80 | + |
| 81 | +def print_report(report): |
| 82 | + |
| 83 | +# report=cal_curr_price(folio_file, price_file) |
| 84 | + headers = ('Name', 'Shares', 'Price', 'Change') |
| 85 | + print('%10s %10s %10s %10s' %headers) |
| 86 | + print(('-' * 10 + ' ') * len(headers)) |
| 87 | + for row in report: |
| 88 | + # **using tuple |
| 89 | + print('%10s %10d $%9.2f %10.2f' % row) |
| 90 | + |
| 91 | + # **using dict |
| 92 | + # print('%10s %10d %10.2f %10.2f' % |
| 93 | +# (row['name'], row['shares'], row['price'], row['change'])) |
| 94 | + return |
| 95 | + |
| 96 | +def pf_report(portfile, pricefile): |
| 97 | +# portfolio = parse_csv(portfile, select=['name','shares','price'], types=[str, int, float]) |
| 98 | +# prices = parse_csv(pricefile, types=[str, float], has_header=False) |
| 99 | + |
| 100 | + with open(portfile, 'rt') as pf1: |
| 101 | + portfolio = parse_csv(pf1, select=['name','shares','price'], types=[str, int, float]) |
| 102 | + |
| 103 | + with open(pricefile, 'r') as pf2: |
| 104 | + prices = parse_csv(pf2, types=[str, float], has_header=False) |
| 105 | + |
| 106 | + report = make_report(portfolio, prices) |
| 107 | + print_report(report) |
| 108 | + # portfolio_report('work/data/portfolio.csv', 'work/data/prices.csv') |
| 109 | + |
| 110 | +def main(args): |
| 111 | + if len(args) != 3: |
| 112 | + raise SystemExit('Usage: %s portfile pricefile' % args[0]) |
| 113 | + pf_report(args[1], args[2]) |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + import sys |
| 117 | + main(sys.argv) |
0 commit comments