Total Complexity | 13 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 95.24% |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | # coding: utf-8 |
||
4 | 1 | class BaseStructure(object): |
|
5 | 1 | __slots__ = [] |
|
6 | |||
7 | 1 | def __eq__(self, other): |
|
8 | 1 | if not isinstance(other, self.__class__): |
|
9 | 1 | return NotImplemented |
|
10 | |||
11 | 1 | if self.__class__ != other.__class__: |
|
12 | return False |
||
13 | |||
14 | 1 | return all([ |
|
15 | getattr(self, x) == getattr(other, x) |
||
16 | for x in self.__slots__ |
||
17 | ]) |
||
18 | |||
19 | 1 | def __ne__(self, other): |
|
20 | 1 | return not (self == other) |
|
21 | |||
22 | 1 | def __hash__(self): |
|
23 | 1 | return hash(tuple( |
|
24 | getattr(self, x) for x in self.__slots__ |
||
25 | )) |
||
26 | |||
27 | 1 | def to_primitive(self, context=None): |
|
28 | 1 | fields = ((key, getattr(self, key)) for key in self.__slots__) |
|
29 | 1 | return { |
|
30 | key: self._to_primitive(value, context) |
||
31 | for key, value in fields |
||
32 | } |
||
33 | |||
34 | 1 | @staticmethod |
|
35 | def _to_primitive(instance, context): |
||
36 | 1 | if hasattr(instance, 'to_primitive'): |
|
37 | 1 | return instance.to_primitive(context) |
|
38 | 1 | elif hasattr(instance, 'isoformat'): |
|
39 | 1 | return instance.isoformat() |
|
40 | else: |
||
41 | return instance |
||
42 |