ex 7.6
This commit is contained in:
parent
7b881b81f4
commit
1f4b203f61
7
stock.py
7
stock.py
@ -1,5 +1,4 @@
|
|||||||
from structure import Structure
|
from structure import Structure
|
||||||
from validate import PositiveFloat, PositiveInteger, String
|
|
||||||
|
|
||||||
|
|
||||||
class Stock(Structure):
|
class Stock(Structure):
|
||||||
@ -19,3 +18,9 @@ class Stock(Structure):
|
|||||||
def from_row(cls, row):
|
def from_row(cls, row):
|
||||||
rowdata = [func(val) for func, val in zip(cls._types, row)]
|
rowdata = [func(val) for func, val in zip(cls._types, row)]
|
||||||
return cls(*rowdata)
|
return cls(*rowdata)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from reader import read_csv_as_instances
|
||||||
|
|
||||||
|
portfolio = read_csv_as_instances('Data/portfolio.csv', Stock)
|
||||||
|
|||||||
17
structure.py
17
structure.py
@ -1,3 +1,5 @@
|
|||||||
|
from collections import ChainMap
|
||||||
|
|
||||||
from validate import Validator, validated
|
from validate import Validator, validated
|
||||||
|
|
||||||
|
|
||||||
@ -8,14 +10,25 @@ def validate_attributes(cls):
|
|||||||
validators.append(val)
|
validators.append(val)
|
||||||
elif callable(val) and val.__annotations__:
|
elif callable(val) and val.__annotations__:
|
||||||
setattr(cls, name, validated(val))
|
setattr(cls, name, validated(val))
|
||||||
cls._fields = tuple(val.name for val in validators)
|
cls._fields = tuple(v.name for v in validators)
|
||||||
cls._types = tuple(getattr(v, 'expected_type', lambda x: x) for v in validators)
|
cls._types = tuple(getattr(v, 'expected_type', lambda x: x) for v in validators)
|
||||||
if cls._fields:
|
if cls._fields:
|
||||||
cls.create_init()
|
cls.create_init()
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
|
||||||
class Structure:
|
class StructureMeta(type):
|
||||||
|
@classmethod
|
||||||
|
def __prepare__(meta, clsname, bases):
|
||||||
|
return ChainMap({}, Validator.validators)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __new__(meta, name, bases, methods):
|
||||||
|
methods = methods.maps[0]
|
||||||
|
return super().__new__(meta, name, bases, methods)
|
||||||
|
|
||||||
|
|
||||||
|
class Structure(metaclass=StructureMeta):
|
||||||
_fields = ()
|
_fields = ()
|
||||||
|
|
||||||
def __init_subclass__(cls):
|
def __init_subclass__(cls):
|
||||||
|
|||||||
12
validate.py
12
validate.py
@ -3,9 +3,15 @@ from functools import wraps
|
|||||||
|
|
||||||
|
|
||||||
class Validator:
|
class Validator:
|
||||||
|
validators = {}
|
||||||
|
|
||||||
def __init__(self, name=None):
|
def __init__(self, name=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __init_subclass__(cls):
|
||||||
|
cls.validators[cls.__name__] = cls
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def check(cls, value):
|
def check(cls, value):
|
||||||
return value
|
return value
|
||||||
@ -28,9 +34,9 @@ class Typed(Validator):
|
|||||||
|
|
||||||
|
|
||||||
_typed_classes = [
|
_typed_classes = [
|
||||||
('Integer', 'int'),
|
('Integer', int),
|
||||||
('Float', 'float'),
|
('Float', float),
|
||||||
('String', 'str'),
|
('String', str),
|
||||||
]
|
]
|
||||||
|
|
||||||
globals().update((name, type(name, (Typed,), {'expected_type': ty}))
|
globals().update((name, type(name, (Typed,), {'expected_type': ty}))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user