From 26d175ba930e634307527b041ae814c9a5c59015 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 7 Jan 2024 16:31:19 -0600 Subject: [PATCH] ex 7.4 --- structure.py | 5 +++++ validate.py | 16 +++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/structure.py b/structure.py index 0bf6c73..0f81c0d 100644 --- a/structure.py +++ b/structure.py @@ -39,3 +39,8 @@ class Structure: locs = {} exec(code, locs) cls.__init__ = locs['__init__'] + + +def typed_structure(clsname, **validators): + cls = type(clsname, (Structure,), validators) + return cls diff --git a/validate.py b/validate.py index 77f3dc7..dc9cd60 100644 --- a/validate.py +++ b/validate.py @@ -27,16 +27,14 @@ class Typed(Validator): return super().check(value) -class Integer(Typed): - expected_type = int +_typed_classes = [ + ('Integer', 'int'), + ('Float', 'float'), + ('String', 'str'), +] - -class Float(Typed): - expected_type = float - - -class String(Typed): - expected_type = str +globals().update((name, type(name, (Typed,), {'expected_type': ty})) + for name, ty in _typed_classes) class Positive(Validator):