@@ -151,14 +151,6 @@ def set_attribute(self, name, value):
151
151
)
152
152
self .__dict__ ['_data_store' ][name ] = value
153
153
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
-
162
154
def __repr__ (self ):
163
155
"""For `print` and `pprint`"""
164
156
return self .to_str ()
@@ -167,6 +159,14 @@ def __ne__(self, other):
167
159
"""Returns true if both objects are not equal"""
168
160
return not self == other
169
161
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
+
170
170
def __new__ (cls , * args , ** kwargs ):
171
171
# this function uses the discriminator to
172
172
# pick a new schema/class to instantiate because a discriminator
@@ -285,40 +285,39 @@ class ModelSimple(OpenApiModel):
285
285
"""the parent class of models whose type != object in their
286
286
swagger/openapi"""
287
287
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` """
290
290
if name in self .required_properties :
291
291
self .__dict__ [name ] = value
292
292
return
293
293
294
294
self .set_attribute (name , value )
295
295
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 """
298
298
if name in self .required_properties :
299
299
return self .__dict__ [name ]
300
300
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 )
303
307
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 )
308
308
raise ApiAttributeError (
309
309
"{0} has no attribute '{1}'" .format (
310
310
type (self ).__name__ , name ),
311
- [name ]
311
+ [e for e in [ self . _path_to_item , name ] if e ]
312
312
)
313
313
314
314
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`"""
316
316
if name in self .required_properties :
317
317
return name in self .__dict__
318
318
319
319
return name in self .__dict__ ['_data_store' ]
320
320
321
-
322
321
def to_str (self ):
323
322
"""Returns the string representation of the model"""
324
323
return str (self .value )
@@ -341,40 +340,39 @@ class ModelNormal(OpenApiModel):
341
340
"""the parent class of models whose type == object in their
342
341
swagger/openapi"""
343
342
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` """
346
345
if name in self .required_properties :
347
346
self .__dict__ [name ] = value
348
347
return
349
348
350
349
self .set_attribute (name , value )
351
350
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 """
354
353
if name in self .required_properties :
355
354
return self .__dict__ [name ]
356
355
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 )
359
362
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 )
364
363
raise ApiAttributeError (
365
364
"{0} has no attribute '{1}'" .format (
366
365
type (self ).__name__ , name ),
367
- [name ]
366
+ [e for e in [ self . _path_to_item , name ] if e ]
368
367
)
369
368
370
369
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`"""
372
371
if name in self .required_properties :
373
372
return name in self .__dict__
374
373
375
374
return name in self .__dict__ ['_data_store' ]
376
375
377
-
378
376
def to_dict (self ):
379
377
"""Returns the model properties as a dict"""
380
378
return model_to_dict (self , serialize = False )
@@ -427,8 +425,8 @@ class ModelComposed(OpenApiModel):
427
425
which contain the value that the key is referring to.
428
426
"""
429
427
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` """
432
430
if name in self .required_properties :
433
431
self .__dict__ [name ] = value
434
432
return
@@ -449,28 +447,22 @@ def __setattr__(self, name, value):
449
447
)
450
448
return None
451
449
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 )
456
450
raise ApiAttributeError (
457
451
"{0} has no attribute '{1}'" .format (
458
452
type (self ).__name__ , name ),
459
- path_to_item
453
+ [ e for e in [ self . _path_to_item , name ] if e ]
460
454
)
461
455
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"""
464
460
if name in self .required_properties :
465
461
return self .__dict__ [name ]
466
462
467
463
# get the attribute from the correct instance
468
464
model_instances = self ._var_name_to_model_instances .get (
469
465
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 )
474
466
values = []
475
467
# A composed model stores child (oneof/anyOf/allOf) models under
476
468
# self._var_name_to_model_instances. A named property can exist in
@@ -484,23 +476,30 @@ def __getattr__(self, name):
484
476
values .append (v )
485
477
len_values = len (values )
486
478
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
492
480
elif len_values == 1 :
493
481
return values [0 ]
494
482
elif len_values > 1 :
495
483
raise ApiValueError (
496
484
"Values stored for property {0} in {1} differ when looking "
497
485
"at self and self's composed instances. All values must be "
498
486
"the same" .format (name , type (self ).__name__ ),
499
- path_to_item
487
+ [ e for e in [ self . _path_to_item , name ] if e ]
500
488
)
501
489
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
+
502
501
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`"""
504
503
505
504
if name in self .required_properties :
506
505
return name in self .__dict__
@@ -515,7 +514,6 @@ def __contains__(self, name):
515
514
516
515
return False
517
516
518
-
519
517
def to_dict (self ):
520
518
"""Returns the model properties as a dict"""
521
519
return model_to_dict (self , serialize = False )
0 commit comments