Completed
Push — master ( b5fe48...f54eca )
by Oleksandr
01:16
created

BaseStructure.__eq__()   A

Complexity

Conditions 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.2
cc 4
crap 4.074
1
# coding: utf-8
2
3
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