-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_debugging.py
More file actions
67 lines (44 loc) · 1.41 KB
/
2_debugging.py
File metadata and controls
67 lines (44 loc) · 1.41 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 1. run this code using a configuration
# 2. debug this code
# 3. add some try except wrappers
# 4. run flake8
# 5. run black
import csv
def read_portfolio(filename):
"""
Read a stock portfolio file into a list of dictionaries with keys
name, shares, and price.
"""
portfolio = []
with open(filename) as f:
rows = csv.reader(f)
# headers = next(rows)
for row in rows:
stock = {
"name": row[1] # suspicious?
,"shares": int(row[1]), "price": float(row[2]),
}
portfolio.append(stock)
return portfolio
def read_prices(filename):
output = {}
with open(filename) as f:
rows = csv.reader(f )
for row in rows:
parsed_price = float(row[1] )
output[row[0]] = parsed_price
return output
if __name__ == "__main__":
portfolio = read_portfolio("./Data/portfolio.csv")
prices = read_prices("./Data/prices.csv") # try prices2
# Calculate the total cost of the portfolio
total_cost = 0.0
for s in portfolio:
total_cost += s["share"] * s["price"]
print("Total cost", total_cost)
# Compute the current value of the portfolio
total_value = 0.0
for s in portfolio:
total_value += s[ "share"] * prices[s["name"]]
print("Current value", total_value )
print("Gain", total_value - total_cost)