Completed
Pull Request — master (#51)
by Thomas
02:39 queued 01:37
created

TypedAttributesHolder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
ccs 23
cts 24
cp 0.9583
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _check_attributes() 0 5 3
A type() 0 4 1
A __repr__() 0 3 3
A __init__() 0 7 3
A __eq__() 0 9 4
A __hash__() 0 2 1
1 1
from .attributesholder import AttributesHolder
2
3 1
class TypedAttributesHolder(AttributesHolder):
4
    """AttributesHolder with a special attributes holding type."""
5 1
    __slots__ = ()
6 1
    _type = None
7 1
    def __init__(self, *args, **attributes):
8
        # Sanity checks
9 1
        if self._type is None or self._possible_attributes is None:
10 1
            raise TypeError('%s is an abstract class.' % self.__class__)
11 1
        super(TypedAttributesHolder, self).__init__(*args, **attributes)
12
13 1
        self._attributes['type'] = self.type
14
15 1
    def _check_attributes(self, attributes, extra=None):
16 1
        if 'type' in attributes:
17 1
            assert attributes.pop('type') == self.type
18 1
        return super(TypedAttributesHolder, self) \
19
                ._check_attributes(attributes, extra)
20
21 1
    @property
22
    def type(self):
23
        """Type of the node."""
24 1
        return self._type
25
26 1
    def __repr__(self):
27 1
        return '<PPP node "%s" %r>' % (self.type,
28
                {x:y for (x,y) in self._attributes.items() if x != 'type'})
29
30 1
    def __eq__(self, other):
31
        """Tests equality with another AttributesHolder instance."""
32 1
        if isinstance(other, dict):
33
            return self.as_dict() == other
34 1
        elif isinstance(other, TypedAttributesHolder) and \
35
                self._type == other._type:
36 1
            return super(TypedAttributesHolder, self).__eq__(other)
37
        else:
38 1
            return NotImplemented
39
40 1
    def __hash__(self):
41 1
        return hash(frozenset(self._attributes.items()))
42
43