ex 3.4
This commit is contained in:
parent
0e72736371
commit
1618e42cbb
35
stock.py
35
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:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user