This commit is contained in:
Mike Bloy 2023-12-03 23:14:51 -06:00
parent 07759f2a42
commit 6f9b537d22
2 changed files with 23 additions and 60 deletions

View File

@ -1,67 +1,12 @@
class Stock:
_types = (str, int, float)
__slots__ = ["name", "_shares", "_price"]
from structure import Structure
def __init__(self, name, shares, price):
self.name = name
self.shares = shares
self.price = price
def __repr__(self):
return f"Stock({self.name!r}, {self.shares!r}, {self.price!r})"
def __eq__(self, other):
if not isinstance(other, Stock):
return False
return (self.name, self.shares, self.price) == (
other.name,
other.shares,
other.price,
)
@classmethod
def from_row(cls, row):
values = [func(val) for func, val in zip(cls._types, row)]
return cls(*values)
class Stock(Structure):
_fields = ('name', 'shares', 'price')
@property
def cost(self):
return self.shares * self.price
@property
def shares(self):
return self._shares
@shares.setter
def shares(self, value):
if not isinstance(value, self._types[1]):
raise TypeError("Expected int")
if value < 0:
raise ValueError("Expected positive integer")
self._shares = value
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if not isinstance(value, self._types[2]):
raise TypeError("Expected float")
if value < 0.0:
raise ValueError("Expected positive float")
self._price = value
def sell(self, num):
self.shares -= num
if self.shares < 0:
self.shares = 0
def print_portfolio(portfolio):
headers = ("name", "shares", "price")
fmtstr = "{0: >10} {1: >10} {2: >10}"
print(fmtstr.format(*headers))
print("{0:->10} {1:->10} {2:->10}".format("", "", ""))
for stock in portfolio:
print(fmtstr.format(stock.name, stock.shares, stock.price))
def sell(self, nshares):
self.shares -= nshares

18
structure.py Normal file
View File

@ -0,0 +1,18 @@
class Structure:
_fields = ()
def __init__(self, *args):
if len(args) != len(self._fields):
raise TypeError(f'Expected {len(self._fields)} arguments')
for pos, field in enumerate(self._fields):
setattr(self, field, args[pos])
def __repr__(self):
args = map(lambda field: f"{field}={getattr(self, field)!r}", self._fields)
return f"{type(self).__name__}({', '.join(args)})"
def __setattr__(self, name, value):
if name in self._fields or name[0] == '_':
super().__setattr__(name, value)
else:
raise AttributeError(f"No attribute {name}")