Passed
Push — master ( 261bc8...13408b )
by Fabio
03:45
created

benedict.dicts.base.base_dict   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 96
dl 0
loc 122
rs 8.8798
c 0
b 0
f 0

22 Methods

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