Visitar URL original
progress · dabeaz-course/practical-python@a061a73 · GitHub
Skip to content

Commit a061a73

Browse files
committed
progress
1 parent b42f46e commit a061a73

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

‎Work/pcost.py‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22
#
33
# Exercise 1.27
44

5-
import csv, sys
5+
import csv
6+
from report import read_portfolio
67

78
def portfolio_cost(filename):
89
'Get total portfolio cost of a csv'
910

10-
total = 0
11-
12-
with open(filename, 'rt') as f:
13-
rows = csv.reader(f)
14-
next(rows)
15-
for row in rows:
16-
try:
17-
total += int(row[1]) * float(row[2])
18-
except ValueError:
19-
print('Something is not valid in row: ', row)
11+
total = sum([s['price']*s['shares'] for s in read_portfolio(filename)])
2012

2113
return total
2214

23-
if len(sys.argv) == 2:
24-
filename = sys.argv[1]
25-
else:
26-
filename = 'Data/portfolio.csv'
15+
def main(argv):
16+
if len(argv) == 2:
17+
filename = argv[1]
18+
else:
19+
filename = 'Data/portfolio.csv'
20+
21+
print('Total cost', portfolio_cost(filename))
22+
2723

28-
cost = portfolio_cost(filename)
29-
print('Total cost', cost)
24+
if __name__ == '__main__':
25+
import sys
26+
main(sys.argv)

‎Work/report.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Exercise 2.4
44

5-
import csv, sys, locale
5+
import csv, locale
66
from fileparse import parse_csv
77

88
locale.setlocale(locale.LC_ALL, '')
@@ -75,8 +75,13 @@ def portfolio_report(portfolio_filename="Data/portfolio.csv", prices_filename="D
7575
print(f'{portfolio_filename:*^43s}')
7676
print_report(report)
7777

78-
if len(sys.argv) > 1:
79-
for file in sys.argv[1:]:
80-
portfolio_report(file)
81-
else:
82-
portfolio_report()
78+
def main(argv):
79+
if len(argv) > 1:
80+
for file in argv[1:]:
81+
portfolio_report(file)
82+
else:
83+
portfolio_report()
84+
85+
if __name__ == '__main__':
86+
import sys
87+
main(sys.argv)

0 commit comments

Comments
 (0)