Coverage for fingerprint_pro_server_api_sdk/base_model.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-21 15:03 +0000

1import pprint 

2from typing import Union, Dict 

3 

4 

5class BaseModel: 

6 """Base class for all models with utility methods.""" 

7 

8 swagger_types: Dict[str, str] = {} 

9 attribute_map: Dict[str, str] = {} 

10 nullable_map: Dict[str, bool] = {} 

11 

12 def to_dict(self) -> Dict[str, Union[list, dict]]: 

13 """Returns the model properties as a dict""" 

14 result = {} 

15 

16 for attr, _ in self.swagger_types.items(): 

17 value = getattr(self, attr) 

18 if isinstance(value, list): 

19 result[attr] = list(map( 

20 lambda x: x.to_dict() if hasattr(x, "to_dict") else x, 

21 value 

22 )) 

23 elif hasattr(value, "to_dict"): 

24 result[attr] = value.to_dict() 

25 elif isinstance(value, Dict): 

26 result[attr] = dict(map( 

27 lambda item: (item[0], item[1].to_dict()) 

28 if hasattr(item[1], "to_dict") else item, 

29 value.items() 

30 )) 

31 elif value is None: 

32 if self.nullable_map[attr]: 

33 result[attr] = None 

34 

35 continue 

36 else: 

37 result[attr] = value 

38 if issubclass(type(self), Dict): 

39 for key, value in self.items(): 

40 result[key] = value 

41 

42 return result 

43 

44 def to_str(self) -> str: 

45 """Returns the string representation of the model""" 

46 return pprint.pformat(self.to_dict()) 

47 

48 def __repr__(self) -> str: 

49 """For `print` and `pprint`""" 

50 return self.to_str() 

51 

52 def __eq__(self, other) -> bool: 

53 """Returns true if both objects are equal""" 

54 if not isinstance(other, type(self)): 

55 return False 

56 return self.to_dict() == other.to_dict() 

57 

58 def __ne__(self, other) -> bool: 

59 """Returns true if both objects are not equal""" 

60 return not self.__eq__(other)