Passed
Branch master (0fb205)
by Oleksandr
03:17
created

commons.structures   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 9
eloc 29
dl 0
loc 39
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseStructure.__hash__() 0 3 1
A BaseStructure._to_primitive() 0 8 3
A BaseStructure.__ne__() 0 2 1
A BaseStructure.__eq__() 0 10 3
A BaseStructure.to_primitive() 0 5 1
1 1
class BaseStructure:
2 1
  __slots__ = []
3
4 1
  def __eq__(self, other):
5 1
    if not isinstance(other, self.__class__):
6 1
      return NotImplemented
7
8 1
    if self.__class__ != other.__class__:
9
      return False
10
11 1
    return all([
12
      getattr(self, x) == getattr(other, x)
13
      for x in self.__slots__
14
    ])
15
16 1
  def __ne__(self, other):
17 1
    return not (self == other)
18
19 1
  def __hash__(self):
20 1
    return hash(tuple(
21
      getattr(self, x) for x in self.__slots__
22
    ))
23
24 1
  def to_primitive(self, context=None):
25 1
    fields = ((key, getattr(self, key)) for key in self.__slots__)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key does not seem to be defined.
Loading history...
26 1
    return {
27
      key: self._to_primitive(value, context)
28
      for key, value in fields
29
    }
30
31 1
  @staticmethod
32 1
  def _to_primitive(instance, context):
33 1
    if hasattr(instance, 'to_primitive'):
34 1
      return instance.to_primitive(context)
35 1
    elif hasattr(instance, 'isoformat'):
36 1
      return instance.isoformat()
37
    else:
38
      return instance
39