Skip to content

Commit 05ef82e

Browse files
committed
before adding class to report
1 parent 3179620 commit 05ef82e

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

Work/koe.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import stock
2+
3+
with stock.open("koe.py", "r", encoding="utf-8") as f:
4+
content = f.read()
5+
print(content)
6+
print("File content read successfully.")
7+

Work/report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# report.py
22
#
3-
# Exercise 2.4 - 2.12 - 3.16
3+
# Exercise 3.18
44
import csv
55
from pprint import pprint
66
from fileparse import parse_csv

Work/stock.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
class Stock:
4+
5+
def __init__(self, name, shares, price):
6+
self.name = name
7+
self.shares = shares
8+
self.price = price
9+
10+
def cost(self):
11+
cost = self.shares * self.price
12+
return cost
13+
14+
def sell(self, cnt):
15+
self.shares -= cnt
16+
17+
def buy(self, cnt):
18+
self.shares += cnt
19+
20+
def update_price(self, price):
21+
self.price = price
22+
23+

Work/using_stock.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import fileparse
3+
import stock
4+
5+
6+
a = stock.Stock('GOOG',100,490.10)
7+
print(a.name)
8+
9+
b = stock.Stock('AAPL', 50, 122.34)
10+
c = stock.Stock('IBM', 75, 91.75)
11+
12+
print(b.shares * b.price)
13+
14+
stocks = [a, b, c]
15+
16+
17+
for s in stocks:
18+
print(f'{s.name:>10s} {s.shares:>10d} {s.price:>10.2f}')
19+
20+
print(a.cost())
21+
22+
a.sell(25)
23+
24+
print(a.shares)
25+
print(a.cost())
26+
27+
28+
29+
with open ('.\\Data\\portfolio.csv') as lines:
30+
portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str,int,float])
31+
32+
print(portdicts)
33+
34+
portfolio = [ stock.Stock(d['name'], d['shares'], d['price']) for d in portdicts]
35+
36+
pf_cost = sum([s.cost() for s in portfolio])
37+
print (f'Total cost: {pf_cost}')
38+
39+

0 commit comments

Comments
 (0)