@@ -19,17 +19,22 @@ def read_prices(filename):
1919 prices = parse_csv (f , types = [str ,float ], has_headers = False )
2020 return dict (prices )
2121
22- def read_portfolio (filename ):
22+ def read_portfolio (filename , ** opts ):
2323 """
2424 Reads a stock portfolio from a CSV file with handling for missing files.
2525 Returns a list of Stock objects.
2626 """
2727
2828 with open (filename ) as f :
29- portdicts = parse_csv (f , types = [str , int , float ])
29+ portdicts = parse_csv (f ,
30+ select = ['name' ,'shares' ,'price' ],
31+ types = [str , int , float ],
32+ ** opts )
3033
31- portfolio = [ Stock (d ['name' ], d ['shares' ], d ['price' ]) for d in portdicts ]
32- return Portfolio (portfolio )
34+ # stock_data = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts]
35+ stock_data = [ Stock (** d ) for d in portdicts ]
36+
37+ return Portfolio (stock_data )
3338
3439def make_report (portfolio , prices ):
3540 '''
@@ -91,15 +96,15 @@ def main(argv):
9196 pf_file = argv [1 ]
9297 price_file = argv [2 ]
9398 fmt = argv [3 ] if len (argv ) > 3 else 'txt' # Default to 'txt' if format is not provided
94-
99+
95100 portfolio_report (pf_file , price_file , fmt )
96101
97102
98103if __name__ == '__main__' :
99104 import sys
100105 # print('this prints when file is executed as main, not imported')
101106 if len (sys .argv ) not in [3 , 4 ]:
102- raise SystemExit (f'Usage: { sys .argv [0 ]} ' 'portfile pricefile < format> ' )
107+ raise SystemExit (f'Usage: { sys .argv [0 ]} ' 'portfile pricefile [ format] ' )
103108
104109 main (sys .argv )
105110
0 commit comments