From 1618e42cbbb5a0a0493a5e192efa38f5b786165e Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sat, 28 Oct 2023 17:02:47 -0500 Subject: [PATCH] ex 3.4 --- stock.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/stock.py b/stock.py index d3dccbf..2bbc98c 100644 --- a/stock.py +++ b/stock.py @@ -1,10 +1,6 @@ -import csv - -from Decimal import Decimal - - class Stock: - types = (str, int, float) + _types = (str, int, float) + __slots__ = ["name", "_shares", "_price"] def __init__(self, name, shares, price): self.name = name @@ -13,12 +9,37 @@ class Stock: @classmethod def from_row(cls, row): - values = [func(val) for func, val in zip(cls.types, row)] + values = [func(val) for func, val in zip(cls._types, row)] return cls(*values) + @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: