@@ -38,9 +38,9 @@ class MyModel(Model):
3838
3939 def __init__ (self , ** kwargs ):
4040 super (Model , self ).__init__ ()
41- [setattr (self , key , value ) for key , value in kwargs .items ()]
42- [self ._set_default (k ) for k in self .__attributes__ .keys ()
43- if k not in kwargs .keys ()]
41+ [setattr (self , name , value ) for name , value in kwargs .items ()]
42+ [self ._set_default (name ) for name in self .__attributes__ .keys ()
43+ if name not in kwargs .keys ()]
4444
4545 def __iter__ (self ):
4646 """Iterate through the model's key, value pairs.
@@ -69,6 +69,21 @@ def __setattr__(self, name, value):
6969 value = self ._validate_value (name , value )
7070 super (Model , self ).__setattr__ (name , value )
7171
72+ def __getattribute__ (self , name ):
73+ """Return the attribute from the model if it is set, otherwise
74+ returning the default if one is set.
75+
76+ :param str name: The attribute name
77+ :rtype: mixed
78+
79+ """
80+ try :
81+ return super (Model , self ).__getattribute__ (name )
82+ except AttributeError :
83+ if name in self .__attributes__ :
84+ return self .__attributes__ [name ].get ('default' , None )
85+ raise
86+
7287 def _maybe_cast_value (self , name ):
7388 """Return the attribute value, possibly cast to a different type if
7489 the ``cast_to`` item is set in the attribute definition.
@@ -123,28 +138,29 @@ def _validate_value(self, name, value):
123138 :raises: ValueError
124139
125140 """
126- if value is None and self ._required_attr (name ):
127- raise ValueError ('Attribute "{}" is required' .format (name ))
128-
129- if value is not None :
130- if not isinstance (value , self .__attributes__ [name ].get ('type' )):
131- cast_from = self .__attributes__ [name ].get ('cast_from' )
132- if cast_from and isinstance (value , cast_from ):
133- value = self .__attributes__ [name ]['type' ](value )
134- else :
135- raise TypeError (
136- 'Attribute "{}" must be of type {} not {}' .format (
137- name , self .__attributes__ [name ]['type' ].__name__ ,
138- value .__class__ .__name__ ))
139-
140- if self .__attributes__ [name ].get ('enum' ) \
141- and value not in self .__attributes__ [name ]['enum' ]:
142- raise ValueError (
143- 'Attribute "{}" value {!r} not valid' .format (name , value ))
141+ if value is None :
142+ if self ._required_attr (name ):
143+ raise ValueError ('Attribute "{}" is required' .format (name ))
144+ return
145+
146+ if not isinstance (value , self .__attributes__ [name ].get ('type' )):
147+ cast_from = self .__attributes__ [name ].get ('cast_from' )
148+ if cast_from and isinstance (value , cast_from ):
149+ value = self .__attributes__ [name ]['type' ](value )
150+ else :
151+ raise TypeError (
152+ 'Attribute "{}" must be of type {} not {}' .format (
153+ name , self .__attributes__ [name ]['type' ].__name__ ,
154+ value .__class__ .__name__ ))
155+
156+ if self .__attributes__ [name ].get ('enum' ) \
157+ and value not in self .__attributes__ [name ]['enum' ]:
158+ raise ValueError (
159+ 'Attribute "{}" value {!r} not valid' .format (name , value ))
144160
145161 validator = self .__attributes__ [name ].get ('validator' )
146162 if callable (validator ):
147- if not validator (value ):
163+ if not validator (value , self ):
148164 raise ValueError (
149165 'Attribute "{}" value {!r} did not validate' .format (
150166 name , value ))
0 commit comments