ex 7.3
This commit is contained in:
parent
a62871c924
commit
fc5fc23c5d
13
stock.py
13
stock.py
@ -1,14 +1,21 @@
|
||||
from structure import Structure
|
||||
from validate import PositiveFloat, PositiveInteger, String
|
||||
|
||||
|
||||
class Stock(Structure):
|
||||
_fields = ('name', 'shares', 'price')
|
||||
_types = ()
|
||||
name = String()
|
||||
shares = PositiveInteger()
|
||||
price = PositiveFloat()
|
||||
|
||||
@property
|
||||
def cost(self):
|
||||
return self.shares * self.price
|
||||
|
||||
def sell(self, nshares):
|
||||
def sell(self, nshares: PositiveInteger):
|
||||
self.shares -= nshares
|
||||
|
||||
Stock.create_init()
|
||||
@classmethod
|
||||
def from_row(cls, row):
|
||||
rowdata = [func(val) for func, val in zip(cls._types, row)]
|
||||
return cls(*rowdata)
|
||||
|
||||
20
structure.py
20
structure.py
@ -1,10 +1,26 @@
|
||||
import inspect
|
||||
import sys
|
||||
from validate import Validator, validated
|
||||
|
||||
|
||||
def validate_attributes(cls):
|
||||
validators = []
|
||||
for name, val in vars(cls).items():
|
||||
if isinstance(val, Validator):
|
||||
validators.append(val)
|
||||
elif callable(val) and val.__annotations__:
|
||||
setattr(cls, name, validated(val))
|
||||
cls._fields = tuple(val.name for val in validators)
|
||||
cls._types = tuple(getattr(v, 'expected_type', lambda x: x) for v in validators)
|
||||
if cls._fields:
|
||||
cls.create_init()
|
||||
return cls
|
||||
|
||||
|
||||
class Structure:
|
||||
_fields = ()
|
||||
|
||||
def __init_subclass__(cls):
|
||||
validate_attributes(cls)
|
||||
|
||||
def __repr__(self):
|
||||
args = map(lambda field: f"{field}={getattr(self, field)!r}", self._fields)
|
||||
return f"{type(self).__name__}({', '.join(args)})"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user