forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcost314.py
More file actions
33 lines (21 loc) · 792 Bytes
/
pcost314.py
File metadata and controls
33 lines (21 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# Exercise 3.14
from report import read_portfolio
def portfolio_cost(filename):
''' Calculate total cost of portofolio
:param filename: The name of the CSV file containing the portfolio data.
:return: The total cost of the portfolio as a float.
'''
portfolio = read_portfolio(filename) # Portfolio class object
# return sum([i['shares'] * i['price'] for i in portfolio ])
# return sum([i.shares * i.price for i in portfolio ])
return portfolio.total_cost
def main(argv):
pf_file = argv[1]
cost = portfolio_cost(pf_file)
print(f'Total cost of portfolio: ${cost:0.2f}')
if __name__ == '__main__':
import sys
if len(sys.argv) != 2:
raise SystemExit(f'Usage: {sys.argv[0]} ' 'portfoliofile')
main(sys.argv)