1+ #!/usr/bin/env python3
2+
13# report.py
24#
35# Exercise 2.4
46import csv
7+ import fileparse
8+ import stock
9+ import tableformat
510
611def read_portfolio (filename ):
7- portfolio = []
8-
9- with open (filename , 'rt' ) as f :
10- rows = csv .reader (f )
11- headers = next (rows )
12- for row in rows :
13- record = dict (zip (headers , row ))
14- holding = record
15- portfolio .append (holding )
16- return portfolio
12+ with open (filename ) as f :
13+ portdicts = fileparse .parse_csv (f , select = ['name' , 'shares' , 'price' ], types = [str , int , float ])
14+ portfolio = [ stock .Stock (d ['name' ], d ['shares' ], d ['price' ]) for d in portdicts ]
15+ return portfolio
1716
1817
1918def read_prices (filename ):
20- prices = {}
21-
22- f = open (filename , 'rt' )
23- lines = csv .reader (f )
24- for line in lines :
25- try :
26- prices [line [0 ]] = float (line [1 ])
27- except IndexError :
28- pass
29- f .close ()
30- return prices
19+ with open (filename ) as f :
20+ return dict (fileparse .parse_csv (f , types = [str , float ], has_headers = False ))
3121
3222
3323def make_report (portfolio , prices ):
3424 updated_portfolio = []
3525
3626 for holding in portfolio :
37- name = holding [ ' name' ]
38- shares = int (holding [ ' shares' ] )
27+ name = holding . name
28+ shares = int (holding . shares )
3929 price = prices [name ]
40- change = price - float (holding [ ' price' ] )
30+ change = price - float (holding . price )
4131 updated_portfolio .append ((name , shares , price , change ))
4232
4333 return updated_portfolio
4434
4535
46- def print_report (report ):
47- headers = ('Name' , 'Shares' , 'Price' , 'Change' )
48- separator = '----------'
49- print (f'{ headers [0 ]:>10s} { headers [1 ]:>10s} { headers [2 ]:>10s} { headers [3 ]:>10s} ' )
50- print (f'{ separator :>10s} { separator :>10s} { separator :>10s} { separator :>10s} ' )
36+ def print_report (reportdata , formatter ):
37+ formatter .headings (['Name' ,'Shares' ,'Price' ,'Change' ])
38+ # headers = ('Name', 'Shares', 'Price', 'Change')
39+ # separator = '----------'
40+ # print(f'{headers[0]:>10s} {headers[1]:>10s} {headers[2]:>10s} {headers[3]:>10s}')
41+ # print(f'{separator:>10s} {separator:>10s} {separator:>10s} {separator:>10s}')
42+ for name , shares , price , change in reportdata :
43+ rowdata = [ name , str (shares ), f'{ price :0.2f} ' , f'{ change :0.2f} ' ]
44+ formatter .row (rowdata )
45+ # currency_sym = '$'
46+ # for name, shares, price, change in report:
47+ # money = f'{currency_sym}{price:0.2f}'
48+ # print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}')
5149
52- currency_sym = '$'
53- for name , shares , price , change in report :
54- money = f'{ currency_sym } { price :0.2f} '
55- print (f'{ name :>10s} { shares :>10d} { money :>10s} { change :>10.2f} ' )
5650
57-
58- def portfolio_report ( portfolio_filename , prices_filename ):
51+ def portfolio_report ( portfolio_filename , prices_filename , fmt = 'txt' ):
52+ # Read data files
5953 portfolio = read_portfolio (portfolio_filename )
6054 prices = read_prices (prices_filename )
55+
56+ # Create the report data
6157 report = make_report (portfolio , prices )
62- print_report (report )
6358
6459
65- portfolio_report ('Data/portfolio.csv' , 'Data/prices.csv' )
60+ formatter = tableformat .create_formatter (fmt )
61+ print_report (report , formatter )
62+
63+
64+ def main (argv ):
65+ portfolio_report (argv [1 ], argv [2 ], argv [3 ])
66+
67+
68+ if __name__ == '__main__' :
69+ import sys
70+ main (sys .argv )
0 commit comments