27 lines
591 B
Python
27 lines
591 B
Python
from structure import Structure
|
|
|
|
|
|
class Stock(Structure):
|
|
_types = ()
|
|
name = String()
|
|
shares = PositiveInteger()
|
|
price = PositiveFloat()
|
|
|
|
@property
|
|
def cost(self):
|
|
return self.shares * self.price
|
|
|
|
def sell(self, nshares: PositiveInteger):
|
|
self.shares -= nshares
|
|
|
|
@classmethod
|
|
def from_row(cls, row):
|
|
rowdata = [func(val) for func, val in zip(cls._types, row)]
|
|
return cls(*rowdata)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
from reader import read_csv_as_instances
|
|
|
|
portfolio = read_csv_as_instances('Data/portfolio.csv', Stock)
|