source: buildhelpers/codegen/templates/base_model_.mustache@ 5071137

debian/bullseye
Last change on this file since 5071137 was 43fd35c, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago

Refactored code to have one gnuboat python structure

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[3a89adb]1import pprint
2
3import six
4{{^supportPython2}}
5import typing
6{{/supportPython2}}
7
8from {{packageName}} import util
9{{^supportPython2}}
10
11T = typing.TypeVar('T')
12{{/supportPython2}}
13
14
15class Model(object):
16 # openapiTypes: The key is attribute name and the
17 # value is attribute type.
18 openapi_types = {}
19
20 # attributeMap: The key is attribute name and the
21 # value is json key in definition.
22 attribute_map = {}
23
24 @classmethod
25 def from_dict(cls{{^supportPython2}}: typing.Type[T]{{/supportPython2}}, dikt){{^supportPython2}} -> T{{/supportPython2}}:
26 """Returns the dict as a model"""
27 return util.deserialize_model(dikt, cls)
28
29 def to_dict(self):
30 """Returns the model properties as a dict
31
32 :rtype: dict
33 """
34 result = {}
35
36 for attr, _ in six.iteritems(self.openapi_types):
37 value = getattr(self, attr)
38 if isinstance(value, list):
39 result[attr] = list(map(
40 lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
41 value
42 ))
43 elif hasattr(value, "to_dict"):
44 result[attr] = value.to_dict()
45 elif isinstance(value, dict):
46 result[attr] = dict(map(
47 lambda item: (item[0], item[1].to_dict())
48 if hasattr(item[1], "to_dict") else item,
49 value.items()
50 ))
51 else:
52 result[attr] = value
53
54 return result
55
56 def to_str(self):
57 """Returns the string representation of the model
58
59 :rtype: str
60 """
61 return pprint.pformat(self.to_dict())
62
63 def __repr__(self):
64 """For `print` and `pprint`"""
65 return self.to_str()
66
67 def __eq__(self, other):
68 """Returns true if both objects are equal"""
69 return self.__dict__ == other.__dict__
70
71 def __ne__(self, other):
72 """Returns true if both objects are not equal"""
73 return not self == other
Note: See TracBrowser for help on using the repository browser.