Passed
Push — master ( 497d37...3baf9b )
by Fabio
03:00
created

benedict.dicts.base.base_dict   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 94
dl 0
loc 119
rs 8.96
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseDict.copy() 0 4 2
A BaseDict.__init__() 0 7 3
A BaseDict.get() 0 4 2
A BaseDict.__repr__() 0 4 2
A BaseDict.__eq__() 0 4 2
A BaseDict.__iter__() 0 4 2
A BaseDict.__setitem__() 0 5 2
A BaseDict.keys() 0 4 2
A BaseDict.update() 0 5 2
A BaseDict.values() 0 4 2
A BaseDict.__delitem__() 0 5 2
A BaseDict.clear() 0 5 2
A BaseDict.setdefault() 0 4 2
A BaseDict.dict() 0 4 2
A BaseDict.items() 0 4 2
A BaseDict.__contains__() 0 4 2
A BaseDict.__len__() 0 4 2
A BaseDict.__str__() 0 4 2
A BaseDict.__getitem__() 0 4 2
A BaseDict.__unicode__() 0 4 2
A BaseDict.pop() 0 4 2

How to fix   Complexity   

Complexity

Complex classes like benedict.dicts.base.base_dict often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
3
4
class BaseDict(dict):
5
6
    _dict = None
7
8
    def __init__(self, *args, **kwargs):
9
        if len(args) == 1 and isinstance(args[0], dict):
10
            self._dict = args[0]
11
            super(BaseDict, self).__init__(self._dict)
12
            return
13
        self._dict = None
14
        super(BaseDict, self).__init__(*args, **kwargs)
15
16
    def __contains__(self, key):
17
        if self._dict is not None:
18
            return key in self._dict
19
        return super(BaseDict, self).__contains__(key)
20
21
    def __delitem__(self, key):
22
        if self._dict is not None:
23
            del self._dict[key]
24
            return
25
        super(BaseDict, self).__delitem__(key)
26
27
    def __eq__(self, other):
28
        if self._dict is not None:
29
            return self._dict == other
30
        return super(BaseDict, self).__eq__(other)
31
32
    def __getitem__(self, key):
33
        if self._dict is not None:
34
            return self._dict[key]
35
        return super(BaseDict, self).__getitem__(key)
36
37
    def __iter__(self):
38
        if self._dict is not None:
39
            return iter(self._dict)
40
        return super(BaseDict, self).__iter__()
41
42
    def __len__(self):
43
        if self._dict is not None:
44
            return len(self._dict)
45
        return super(BaseDict, self).__len__()
46
47
    def __repr__(self):
48
        if self._dict is not None:
49
            return repr(self._dict)
50
        return super(BaseDict, self).__repr__()
51
52
    def __setitem__(self, key, value):
53
        if self._dict is not None:
54
            self._dict[key] = value
55
            return
56
        super(BaseDict, self).__setitem__(key, value)
57
58
    def __str__(self):
59
        if self._dict is not None:
60
            return str(self._dict)
61
        return super(BaseDict, self).__str__()
62
63
    def __unicode__(self):
64
        if self._dict is not None:
65
            return unicode(self._dict)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unicode does not seem to be defined.
Loading history...
66
        return super(BaseDict, self).__unicode__()
67
68
    def clear(self):
69
        if self._dict is not None:
70
            self._dict.clear()
71
            return
72
        super(BaseDict, self).clear()
73
74
    def copy(self):
75
        if self._dict is not None:
76
            return self._dict.copy()
77
        return super(BaseDict, self).copy()
78
79
    def dict(self):
80
        if self._dict is not None:
81
            return self._dict
82
        return self
83
84
    def get(self, key, default=None):
85
        if self._dict is not None:
86
            return self._dict.get(key, default)
87
        return super(BaseDict, self).get(key, default)
88
89
    def items(self):
90
        if self._dict is not None:
91
            return self._dict.items()
92
        return super(BaseDict, self).items()
93
94
    def keys(self):
95
        if self._dict is not None:
96
            return self._dict.keys()
97
        return super(BaseDict, self).keys()
98
99
    def pop(self, key, *args):
100
        if self._dict is not None:
101
            return self._dict.pop(key, *args)
102
        return super(BaseDict, self).pop(key, *args)
103
104
    def setdefault(self, key, default=None):
105
        if self._dict is not None:
106
            return self._dict.setdefault(key, default)
107
        return super(BaseDict, self).setdefault(key, default)
108
109
    def update(self, other):
110
        if self._dict is not None:
111
            self._dict.update(other)
112
            return
113
        super(BaseDict, self).update(other)
114
115
    def values(self):
116
        if self._dict is not None:
117
            return self._dict.values()
118
        return super(BaseDict, self).values()
119