python-mastery/stock.py
2024-01-07 16:24:06 -06:00

22 lines
509 B
Python

from structure import Structure
from validate import PositiveFloat, PositiveInteger, String
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)