@@ -151,14 +151,6 @@ def set_attribute(self, name, value):
151151 )
152152 self .__dict__ ['_data_store' ][name ] = value
153153
154- def __setitem__ (self , name , value ):
155- """this allows us to set values with instance[field_name] = val"""
156- self .__setattr__ (name , value )
157-
158- def __getitem__ (self , name ):
159- """this allows us to get a value with val = instance[field_name]"""
160- return self .__getattr__ (name )
161-
162154 def __repr__ (self ):
163155 """For `print` and `pprint`"""
164156 return self .to_str ()
@@ -167,6 +159,14 @@ def __ne__(self, other):
167159 """Returns true if both objects are not equal"""
168160 return not self == other
169161
162+ def __setattr__ (self , attr , value ):
163+ """set the value of an attribute using dot notation: `instance.attr = val`"""
164+ self [attr ] = value
165+
166+ def __getattr__ (self , attr ):
167+ """get the value of an attribute using dot notation: `instance.attr`"""
168+ return self .__getitem__ (attr )
169+
170170 def __new__ (cls , * args , ** kwargs ):
171171 # this function uses the discriminator to
172172 # pick a new schema/class to instantiate because a discriminator
@@ -285,40 +285,39 @@ class ModelSimple(OpenApiModel):
285285 """the parent class of models whose type != object in their
286286 swagger/openapi"""
287287
288- def __setattr__ (self , name , value ):
289- """this allows us to set a value with instance.field_name = val"""
288+ def __setitem__ (self , name , value ):
289+ """set the value of an attribute using square-bracket notation: ` instance[attr] = val` """
290290 if name in self .required_properties :
291291 self .__dict__ [name ] = value
292292 return
293293
294294 self .set_attribute (name , value )
295295
296- def __getattr__ (self , name ):
297- """this allows us to get a value with val = instance.field_name """
296+ def get (self , name , default = None ):
297+ """returns the value of an attribute or some default value if the attribute was not set """
298298 if name in self .required_properties :
299299 return self .__dict__ [name ]
300300
301- if name in self .__dict__ ['_data_store' ]:
302- return self .__dict__ ['_data_store' ][name ]
301+ return self .__dict__ ['_data_store' ].get (name , default )
302+
303+ def __getitem__ (self , name ):
304+ """get the value of an attribute using square-bracket notation: `instance[attr]`"""
305+ if name in self :
306+ return self .get (name )
303307
304- path_to_item = []
305- if self ._path_to_item :
306- path_to_item .extend (self ._path_to_item )
307- path_to_item .append (name )
308308 raise ApiAttributeError (
309309 "{0} has no attribute '{1}'" .format (
310310 type (self ).__name__ , name ),
311- [name ]
311+ [e for e in [ self . _path_to_item , name ] if e ]
312312 )
313313
314314 def __contains__ (self , name ):
315- """this allows us to use `in` operator : `'attr' in instance`"""
315+ """used by `in` operator to check if an attrbute value was set in an instance : `'attr' in instance`"""
316316 if name in self .required_properties :
317317 return name in self .__dict__
318318
319319 return name in self .__dict__ ['_data_store' ]
320320
321-
322321 def to_str (self ):
323322 """Returns the string representation of the model"""
324323 return str (self .value )
@@ -341,40 +340,39 @@ class ModelNormal(OpenApiModel):
341340 """the parent class of models whose type == object in their
342341 swagger/openapi"""
343342
344- def __setattr__ (self , name , value ):
345- """this allows us to set a value with instance.field_name = val"""
343+ def __setitem__ (self , name , value ):
344+ """set the value of an attribute using square-bracket notation: ` instance[attr] = val` """
346345 if name in self .required_properties :
347346 self .__dict__ [name ] = value
348347 return
349348
350349 self .set_attribute (name , value )
351350
352- def __getattr__ (self , name ):
353- """this allows us to get a value with val = instance.field_name """
351+ def get (self , name , default = None ):
352+ """returns the value of an attribute or some default value if the attribute was not set """
354353 if name in self .required_properties :
355354 return self .__dict__ [name ]
356355
357- if name in self .__dict__ ['_data_store' ]:
358- return self .__dict__ ['_data_store' ][name ]
356+ return self .__dict__ ['_data_store' ].get (name , default )
357+
358+ def __getitem__ (self , name ):
359+ """get the value of an attribute using square-bracket notation: `instance[attr]`"""
360+ if name in self :
361+ return self .get (name )
359362
360- path_to_item = []
361- if self ._path_to_item :
362- path_to_item .extend (self ._path_to_item )
363- path_to_item .append (name )
364363 raise ApiAttributeError (
365364 "{0} has no attribute '{1}'" .format (
366365 type (self ).__name__ , name ),
367- [name ]
366+ [e for e in [ self . _path_to_item , name ] if e ]
368367 )
369368
370369 def __contains__ (self , name ):
371- """this allows us to use `in` operator : `'attr' in instance`"""
370+ """used by `in` operator to check if an attrbute value was set in an instance : `'attr' in instance`"""
372371 if name in self .required_properties :
373372 return name in self .__dict__
374373
375374 return name in self .__dict__ ['_data_store' ]
376375
377-
378376 def to_dict (self ):
379377 """Returns the model properties as a dict"""
380378 return model_to_dict (self , serialize = False )
@@ -427,8 +425,8 @@ class ModelComposed(OpenApiModel):
427425 which contain the value that the key is referring to.
428426 """
429427
430- def __setattr__ (self , name , value ):
431- """this allows us to set a value with instance.field_name = val"""
428+ def __setitem__ (self , name , value ):
429+ """set the value of an attribute using square-bracket notation: ` instance[attr] = val` """
432430 if name in self .required_properties :
433431 self .__dict__ [name ] = value
434432 return
@@ -449,28 +447,22 @@ def __setattr__(self, name, value):
449447 )
450448 return None
451449
452- path_to_item = []
453- if self ._path_to_item :
454- path_to_item .extend (self ._path_to_item )
455- path_to_item .append (name )
456450 raise ApiAttributeError (
457451 "{0} has no attribute '{1}'" .format (
458452 type (self ).__name__ , name ),
459- path_to_item
453+ [ e for e in [ self . _path_to_item , name ] if e ]
460454 )
461455
462- def __getattr__ (self , name ):
463- """this allows us to get a value with val = instance.field_name"""
456+ __unset_attribute_value__ = object ()
457+
458+ def get (self , name , default = None ):
459+ """returns the value of an attribute or some default value if the attribute was not set"""
464460 if name in self .required_properties :
465461 return self .__dict__ [name ]
466462
467463 # get the attribute from the correct instance
468464 model_instances = self ._var_name_to_model_instances .get (
469465 name , self ._additional_properties_model_instances )
470- path_to_item = []
471- if self ._path_to_item :
472- path_to_item .extend (self ._path_to_item )
473- path_to_item .append (name )
474466 values = []
475467 # A composed model stores child (oneof/anyOf/allOf) models under
476468 # self._var_name_to_model_instances. A named property can exist in
@@ -484,23 +476,30 @@ def __getattr__(self, name):
484476 values .append (v )
485477 len_values = len (values )
486478 if len_values == 0 :
487- raise ApiAttributeError (
488- "{0} has no attribute '{1}'" .format (
489- type (self ).__name__ , name ),
490- path_to_item
491- )
479+ return default
492480 elif len_values == 1 :
493481 return values [0 ]
494482 elif len_values > 1 :
495483 raise ApiValueError (
496484 "Values stored for property {0} in {1} differ when looking "
497485 "at self and self's composed instances. All values must be "
498486 "the same" .format (name , type (self ).__name__ ),
499- path_to_item
487+ [ e for e in [ self . _path_to_item , name ] if e ]
500488 )
501489
490+ def __getitem__ (self , name ):
491+ """get the value of an attribute using square-bracket notation: `instance[attr]`"""
492+ value = self .get (name , self .__unset_attribute_value__ )
493+ if value is self .__unset_attribute_value__ :
494+ raise ApiAttributeError (
495+ "{0} has no attribute '{1}'" .format (
496+ type (self ).__name__ , name ),
497+ [e for e in [self ._path_to_item , name ] if e ]
498+ )
499+ return value
500+
502501 def __contains__ (self , name ):
503- """this allows us to use `in` operator : `'attr' in instance`"""
502+ """used by `in` operator to check if an attrbute value was set in an instance : `'attr' in instance`"""
504503
505504 if name in self .required_properties :
506505 return name in self .__dict__
@@ -515,7 +514,6 @@ def __contains__(self, name):
515514
516515 return False
517516
518-
519517 def to_dict (self ):
520518 """Returns the model properties as a dict"""
521519 return model_to_dict (self , serialize = False )
0 commit comments