Coverage for fingerprint_pro_server_api_sdk/base_model.py: 100%
32 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-09 17:50 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-09 17:50 +0000
1import pprint
2from typing import Union, Dict
5class BaseModel:
6 """Base class for all models with utility methods."""
8 swagger_types: Dict[str, str] = {}
9 attribute_map: Dict[str, str] = {}
11 def to_dict(self) -> Dict[str, Union[list, dict]]:
12 """Returns the model properties as a dict"""
13 result = {}
15 for attr, _ in self.swagger_types.items():
16 value = getattr(self, attr)
17 if isinstance(value, list):
18 result[attr] = list(map(
19 lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
20 value
21 ))
22 elif hasattr(value, "to_dict"):
23 result[attr] = value.to_dict()
24 elif isinstance(value, Dict):
25 result[attr] = dict(map(
26 lambda item: (item[0], item[1].to_dict())
27 if hasattr(item[1], "to_dict") else item,
28 value.items()
29 ))
30 elif value is None:
31 continue
32 else:
33 result[attr] = value
34 if issubclass(type(self), Dict):
35 for key, value in self.items():
36 result[key] = value
38 return result
40 def to_str(self) -> str:
41 """Returns the string representation of the model"""
42 return pprint.pformat(self.to_dict())
44 def __repr__(self) -> str:
45 """For `print` and `pprint`"""
46 return self.to_str()
48 def __eq__(self, other) -> bool:
49 """Returns true if both objects are equal"""
50 if not isinstance(other, type(self)):
51 return False
52 return self.to_dict() == other.to_dict()
54 def __ne__(self, other) -> bool:
55 """Returns true if both objects are not equal"""
56 return not self.__eq__(other)