Skip to content

Commit e9a5890

Browse files
committed
exersices 4.3 done
1 parent 81c2ef3 commit e9a5890

3 files changed

Lines changed: 41 additions & 15 deletions

File tree

mynotes/report.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ def print_report(report, total_cost, total_gain_lost, formatter):
8989
'Price',
9090
'Change',
9191
'Gain/Lost']
92-
# headers_len = len(headers)
93-
# print('%10s '*headers_len % headers)
94-
# print(('-'*10 + ' ')*headers_len)
9592

9693
formatter.headings(headers)
9794

@@ -118,8 +115,6 @@ def print_report(report, total_cost, total_gain_lost, formatter):
118115
' '*10,
119116
'Gain/lost',
120117
f'{total_gain_lost:>10.2f}']
121-
# print(('-'*10 + ' ')*len(footer))
122-
# print('%10s '*len(footer) % footer)
123118
formatter.footings(footers)
124119

125120
def portfolio_report(portfoliofile, pricefile, fmt='txt'):
@@ -134,14 +129,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'):
134129
report, total_cost, total_gain_lost = make_report_data(portfolio, prices)
135130

136131
# Print it out
137-
if fmt == 'txt':
138-
formatter = tableformat.TextTableFormatter()
139-
elif fmt == 'csv':
140-
formatter = tableformat.CSVTableFormatter()
141-
elif fmt == 'html':
142-
formatter = tableformat.HTMLTableFormatter()
143-
else:
144-
raise RuntimeError(f'Unknow format {fmt}')
132+
formatter = tableformat.createformatter(fmt)
145133

146134
print_report(report, total_cost, total_gain_lost, formatter)
147135

mynotes/stock.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ def cost(self):
1212
def sell(self, count):
1313
if count > self.shares:
1414
raise NameError(f'Can\'t sell {count}. Shares not enogh')
15-
self.shares -= count
15+
self.shares -= count
16+
17+
def __repr__(self):
18+
return f'Stock(\'{self.name}\', {self.shares:d}, {self.price:0.2f})'

mynotes/tableformat.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,39 @@ def footings(self, footers):
6464
print('<tr>')
6565
for f in footers:
6666
print(f'<th>{f:s}</th>', end='')
67-
print('</tr>')
67+
print('</tr>')
68+
69+
def create_formatter(fmt):
70+
if fmt == 'txt':
71+
formatter = TextTableFormatter()
72+
elif fmt == 'csv':
73+
formatter = CSVTableFormatter()
74+
elif fmt == 'html':
75+
formatter = HTMLTableFormatter()
76+
else:
77+
raise RuntimeError(f'Unknow format {fmt}')
78+
return formatter
79+
80+
def print_table(collection, fields, formatter):
81+
82+
formatter.headings(fields)
83+
for item in collection:
84+
rowdata = []
85+
# print(item)
86+
for field in fields:
87+
strval = value_format(getattr(item, field))
88+
# print(strval)
89+
rowdata.append(strval)
90+
formatter.row(rowdata)
91+
92+
def value_format(value):
93+
if isinstance(value, int):
94+
return f'{value:>10d}'
95+
96+
if isinstance(value, float):
97+
return f'{value:>10.2f}'
98+
99+
return f'{value:>10s}'
100+
101+
102+

0 commit comments

Comments
 (0)