forked from XeroAPI/xero-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
69 lines (49 loc) · 1.79 KB
/
Copy pathmodels.py
File metadata and controls
69 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
import pprint
from functools import singledispatch
class BaseModel:
""""""
"""
Attributes:
openapi_types (dict): The key is attribute name
and the value is attribute type.
attribute_map (dict): The key is attribute name
and the value is json key in definition.
"""
openapi_types = {} # child model to define specific mappings
attribute_map = {} # child model to define specific mappings
def to_dict(self):
"""Returns the model properties as a dict"""
return {
key: serialize_to_dict(getattr(self, key))
for key in self.openapi_types.keys()
}
def to_str(self):
"""Returns the string representation of the model"""
return pprint.pformat(self.to_dict())
def __repr__(self):
"""For `print` and `pprint`"""
return self.to_str()
def __eq__(self, other):
"""Returns true if both objects are equal"""
if not isinstance(other, type(self)):
return False
return self.__dict__ == other.__dict__
def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
@singledispatch
def serialize_to_dict(value):
return value
@serialize_to_dict.register(BaseModel)
def serialize_model_to_dict(value):
return value.to_dict()
@serialize_to_dict.register(list)
def serialize_list_to_dict(value):
return [serialize_to_dict(item) for item in value]
@serialize_to_dict.register(tuple)
def serialize_tuple_to_dict(value):
return tuple(serialize_to_dict(item) for item in value)
@serialize_to_dict.register(dict)
def serialize_dict_to_dict(value):
return {key: serialize_to_dict(val) for key, val in value.items()}