The first half of the lab will be a javascript quiz
~/cs2613/labs/L18read_portfolio to the Stock class. @staticmethod
def read_portfolio(filename):
# code from 4.3 goes here
Your completed static method should pass
def test_read_portfolio():
portfolio = Stock.read_portfolio('Data/portfolio.csv')
assert repr(portfolio[0:3]) == \
"[Stock('AA', 100, 32.2), Stock('IBM', 50, 91.1), Stock('CAT', 150, 83.44)]"
repr function does this test demonstrate?Start with following class hierarchy based on Exercises 4.5 and 4.6
class TableFormatter:
def headings(self, headers):
'''
Emit the table headings.
'''
raise NotImplementedError()
def row(self, rowdata):
'''
Emit a single row of table data.
'''
raise NotImplementedError()
class TextTableFormatter(TableFormatter):
'''
Emit a table in plain-text format
'''
def headings(self, headers):
output = ''
for h in headers:
output += f'{h:>10s} '
output+='\n'
output+=(('-'*10 + ' ')*len(headers))
output += '\n'
return output
def row(self, rowdata):
output = ''
for d in rowdata:
output+=f'{d:>10s} '
output += '\n'
return output
def test_text_2():
portfolio=stock.Stock.read_portfolio('Data/portfolio.csv')
formatter= TextTableFormatter()
output= formatter.headings(['Name','Shares','Price', 'Cost'])
for obj in portfolio[0:3]:
output +=formatter.row([obj.name,f'{obj.shares}',
f'{obj.price:0.2f}',f'{obj.cost():0.2f}'])
assert '\n' + output == '''
Name Shares Price Cost
---------- ---------- ---------- ----------
AA 100 32.20 3220.00
IBM 50 91.10 4555.00
CAT 150 83.44 12516.00
'''
Define an appropriate method so that the str function
for Stock instances returns the same output as the
in one (non-heading) row of the given test.
Define a new class StockTableFormatter that uses the str
builtin function for Stock objects and passes the following test
def test_string_1():
portfolio=stock.Stock.read_portfolio('Data/portfolio.csv')
formatter= StockTableFormatter()
output= formatter.headings(['Name','Shares','Price', 'Cost'])
for obj in portfolio[0:3]:
output +=formatter.row(obj)
assert '\n' + output == '''
Name Shares Price Cost
---------- ---------- ---------- ----------
AA 100 32.20 3220.00
IBM 50 91.10 4555.00
CAT 150 83.44 12516.00
'''
test_text_2 and test_string_1 are
sensitve to whitespace, so be careful when copying the strings.