Skip to content

Commit b2368c3

Browse files
committed
end of 4.3
1 parent 8767793 commit b2368c3

5 files changed

Lines changed: 56 additions & 7 deletions

File tree

Work/stock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ def __init__(self, name, shares, price):
77
self.shares = shares
88
self.price = price
99

10+
# without this, the print of a Stock object is not very informative
11+
# [<stock.Stock object at 0x0000022C9F7C9FD0>, <stock.Stock object at ...>]
12+
13+
def __repr__(self):
14+
return f"Stock({self.name}, {self.shares}, {self.price})"
15+
1016
def cost(self):
1117
cost = self.shares * self.price
1218
return cost

Work/tableformat.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ def create_formatter(fmt):
7878
else:
7979
raise ValueError(f'Unknown format: {fmt}')
8080

81+
82+
83+
def print_table(data, cols, fmt):
84+
'''
85+
Print a table of data using the specified format.
86+
87+
:param data: A list of stock objects containing the table data.
88+
:param cols: A list of column names to be used as headings in the table.
89+
:param fmt: A string indicating the desired format ('txt', 'csv', 'html').
90+
'''
91+
92+
fmt.headings(cols)
93+
94+
for d in data:
95+
rowdata = [str(getattr(d, colname)) for colname in cols] # ['AA', 100]
96+
# rowdata = [str(item) for item in rowdata] # ['AA', '100']
97+
98+
fmt.row(rowdata)
99+
100+

Work/using_report.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
pf_file = '.\\Data\\portfolio.csv'
1414

15+
portfolio = r.read_portfolio(pf_file)
16+
print(portfolio)
17+
1518
print(f'{pf_file:-^43s}')
1619
r.portfolio_report(pf_file, price_file)
1720
print()

Work/using_stock.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55

66
a = stock.Stock('GOOG',100,490.10)
7-
print(a.name)
7+
print(a)
8+
columns = ['name', 'shares']
9+
for colname in columns:
10+
print(colname, '=', getattr(a, colname))
11+
812

913
b = stock.Stock('AAPL', 50, 122.34)
1014
c = stock.Stock('IBM', 75, 91.75)
@@ -26,14 +30,14 @@
2630

2731

2832

29-
with open ('.\\Data\\portfolio.csv') as lines:
30-
portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str,int,float])
33+
# with open ('.\\Data\\portfolio.csv') as lines:
34+
# portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str,int,float])
3135

32-
print(portdicts)
36+
# print(portdicts)
3337

34-
portfolio = [ stock.Stock(d['name'], d['shares'], d['price']) for d in portdicts]
38+
# portfolio = [ stock.Stock(d['name'], d['shares'], d['price']) for d in portdicts]
3539

36-
pf_cost = sum([s.cost() for s in portfolio])
37-
print (f'Total cost: {pf_cost}')
40+
# pf_cost = sum([s.cost() for s in portfolio])
41+
# print (f'Total cost: {pf_cost}')
3842

3943

Work/using_tableformat.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
import report as r
4+
import tableformat as tf
5+
6+
7+
pf_file = '.\\Data\\portfolio.csv'
8+
9+
portfolio = r.read_portfolio(pf_file)
10+
formatter = tf.create_formatter('txt')
11+
12+
tf.print_table(portfolio, ['name','shares'], formatter)
13+
14+
tf.print_table(portfolio, ['name','shares','price'], formatter)
15+
16+
tf.print_table(portfolio, ['name','price'], formatter)

0 commit comments

Comments
 (0)