@@ -20,10 +20,10 @@ def create_pair(key: str, typ: type, default=None, container=None) -> property:
2020 def get (self ):
2121 if hasattr (self , '__' + str (key )):
2222 return getattr (self , '__' + str (key ))
23- if default is not None and type (default ) == typ :
23+ if default is not None and ( type (default ) == typ or ( container == list and ( default == [] or type ( default [ 0 ]) == typ ))) :
2424 if container :
2525 if container == list :
26- return [ default ]
26+ return default
2727 else :
2828 raise TypeError ('Unrecognized container: %s' ,
2929 str (container ))
@@ -53,33 +53,55 @@ def config(cls):
5353 vars = []
5454 for k , v in cls .__dict__ .items ():
5555 if isinstance (v , type ):
56+ # V is a type, no default value
5657 v = create_pair (k , v )
5758 vars .append (k )
5859 elif isinstance (v , tuple ) and \
5960 isinstance (v [0 ], type ) and \
6061 isinstance (v [1 ], v [0 ]):
62+ # v is a pair, (type, instance)
6163 v = create_pair (k , v [0 ], v [1 ])
6264 vars .append (k )
6365 elif isinstance (v , list ) and \
6466 isinstance (v [0 ], type ):
67+ # v is a list [type]
6568 v = create_pair (k , v [0 ], container = list )
6669 vars .append (k )
6770 elif isinstance (v , tuple ) and \
6871 isinstance (v [0 ], list ) and \
6972 isinstance (v [0 ][0 ], type ) and \
70- isinstance (v [1 ], list ) and \
71- isinstance (v [1 ][0 ], v [0 ][0 ]):
72- v = create_pair (k , v [0 ][0 ], v [1 ][0 ], container = list )
73- vars .append (k )
73+ isinstance (v [1 ], list ):
74+ # v is a pair, ([type], [instance?])
75+ if len (v [1 ]) > 0 and isinstance (v [1 ][0 ], v [0 ][0 ]): # TODO check all
76+ v = create_pair (k , v [0 ][0 ], v [1 ], container = list )
77+ vars .append (k )
78+ elif v [1 ] == []:
79+ v = create_pair (k , v [0 ][0 ], v [1 ], container = list )
80+ vars .append (k )
81+ else :
82+ print (v )
83+ raise Exception ('Unexpected list instance: %s' % v [1 ][0 ])
7484
7585 new_cls_dict [k ] = v
86+ new_cls_dict ['__init__' ] = __init__config
7687 new_cls_dict ['__repr__' ] = __repr__
7788 new_cls_dict ['_vars' ] = vars
7889 new_cls_dict ['_excludes' ] = []
7990 return type (cls )(cls .__name__ , cls .__bases__ , new_cls_dict )
8091
8192
82- def __init__ (self , ** kwargs ) -> None :
93+ def __init__config (self , ** kwargs ) -> None :
94+ for item in self ._vars :
95+ if item not in kwargs :
96+ log .debug ('WARNING %s unset!' , item )
97+ else :
98+ setattr (self , item , kwargs .get (item ))
99+ for k , v in kwargs .items ():
100+ if k not in self ._vars :
101+ raise Exception ('Attribute not found! %s' % k )
102+
103+
104+ def __init__struct (self , ** kwargs ) -> None :
83105 for item in self ._vars :
84106 if item not in kwargs :
85107 pass
@@ -132,7 +154,7 @@ def struct(cls):
132154 # v = create_pair(k, v[0])
133155
134156 new_cls_dict [k ] = v
135- new_cls_dict ['__init__' ] = __init__
157+ new_cls_dict ['__init__' ] = __init__struct
136158 new_cls_dict ['__repr__' ] = __repr__
137159 new_cls_dict ['_vars' ] = vars
138160 new_cls_dict ['_excludes' ] = excludes
0 commit comments