TraceItem   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 42
Duplicated Lines 33.33 %

Test Coverage

Coverage 75.86%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 14
loc 42
ccs 22
cts 29
cp 0.7586
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _parse_attributes() 0 4 1
B _check_attributes() 14 14 7
A deserialize_attribute() 0 6 2
A __eq__() 0 5 2
A __hash__() 0 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 1
import sys
2 1
import json
3
4 1
from ..nodes import AbstractNode
5 1
from ..exceptions import AttributeNotProvided
6 1
from ..utils import SerializableAttributesHolder
7
8 1
if sys.version_info[0] >= 3:
9 1
    basestring = str
10
11 1
class TraceItem(SerializableAttributesHolder):
12
    """Represents a trace item.
13
    https://github.com/ProjetPP/Documentation/blob/master/module-communication.md#format-of-a-trace-item
14
    """
15 1
    __slots__ = ()
16 1
    _possible_attributes = ('module', 'tree', 'measures', 'times')
17
18 1 View Code Duplication
    def _check_attributes(self, attributes, extra=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
19 1
        super(TraceItem, self)._check_attributes(attributes)
20
        # Allow missing 'time' attribute for now (transitioning)
21 1
        missing_attributes = {'module', 'tree', 'measures'} - set(attributes.keys())
22 1
        if missing_attributes:
23
            raise AttributeNotProvided('Missing attributes: %s' % ', '.join(missing_attributes))
24 1
        if not isinstance(attributes['module'], basestring):
25
            raise TypeError('"module" attribute is not a string.')
26 1
        if not isinstance(attributes['tree'], AbstractNode):
27
            raise TypeError('"tree" attribute is not an AbstractNode.')
28 1
        if not isinstance(attributes['measures'], dict):
29
            raise TypeError('"measures" attribute is not a dict.')
30 1
        if 'times' in attributes and not isinstance(attributes['times'], dict):
31
            raise TypeError('"times" attribute is not a dict.')
32
33 1
    def _parse_attributes(self, attributes):
34
        # Allow missing 'time' attribute for now (transitioning)
35 1
        attributes.setdefault('times', {})
36 1
        super(TraceItem, self)._parse_attributes(attributes)
37
38 1
    def __eq__(self, other):
39 1
        if not isinstance(other, TraceItem):
40
            return False
41
        else:
42 1
            return super(TraceItem, self).__eq__(other)
43
44 1
    def __hash__(self):
45
        return hash((TraceItem, self._attributes))
46
47 1
    @classmethod
48
    def deserialize_attribute(cls, key, value):
49 1
        if key == 'tree':
50 1
            return AbstractNode.deserialize_attribute(key, value)
51
        else:
52
            return value
53