This commit is contained in:
Mike Bloy 2024-01-07 16:24:06 -06:00
parent a62871c924
commit fc5fc23c5d
2 changed files with 28 additions and 5 deletions

View File

@ -1,14 +1,21 @@
from structure import Structure from structure import Structure
from validate import PositiveFloat, PositiveInteger, String
class Stock(Structure): class Stock(Structure):
_fields = ('name', 'shares', 'price') _types = ()
name = String()
shares = PositiveInteger()
price = PositiveFloat()
@property @property
def cost(self): def cost(self):
return self.shares * self.price return self.shares * self.price
def sell(self, nshares): def sell(self, nshares: PositiveInteger):
self.shares -= nshares 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)

View File

@ -1,10 +1,26 @@
import inspect from validate import Validator, validated
import sys
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: class Structure:
_fields = () _fields = ()
def __init_subclass__(cls):
validate_attributes(cls)
def __repr__(self): def __repr__(self):
args = map(lambda field: f"{field}={getattr(self, field)!r}", self._fields) args = map(lambda field: f"{field}={getattr(self, field)!r}", self._fields)
return f"{type(self).__name__}({', '.join(args)})" return f"{type(self).__name__}({', '.join(args)})"