Skip to content

Commit 471950a

Browse files
committed
Refactored BaseDict class.
1 parent d0b5f17 commit 471950a

File tree

1 file changed

+65
-34
lines changed

1 file changed

+65
-34
lines changed

benedict/dicts/base/base_dict.py

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,116 @@
33

44
class BaseDict(dict):
55

6-
_dict = {}
6+
_dict = None
77

88
def __init__(self, *args, **kwargs):
99
if len(args) == 1 and isinstance(args[0], dict):
1010
self._dict = args[0]
11-
else:
12-
self._dict = dict(*args, **kwargs)
13-
super(BaseDict, self).__init__(self._dict)
14-
15-
def __bool__(self):
16-
return bool(self._dict)
11+
super(BaseDict, self).__init__(self._dict)
12+
return
13+
self._dict = None
14+
super(BaseDict, self).__init__(*args, **kwargs)
1715

1816
def __contains__(self, key):
19-
return key in self._dict
17+
if self._dict is not None:
18+
return key in self._dict
19+
return super(BaseDict, self).__contains__(key)
2020

2121
def __delitem__(self, key):
22-
del self._dict[key]
22+
if self._dict is not None:
23+
del self._dict[key]
24+
return
2325
super(BaseDict, self).__delitem__(key)
2426

2527
def __eq__(self, other):
26-
return self._dict == other
28+
if self._dict is not None:
29+
return self._dict == other
30+
return super(BaseDict, self).__eq__(other)
2731

2832
def __getitem__(self, key):
29-
return self._dict[key]
33+
if self._dict is not None:
34+
return self._dict[key]
35+
return super(BaseDict, self).__getitem__(key)
3036

3137
def __iter__(self):
32-
return iter(self._dict)
38+
if self._dict is not None:
39+
return iter(self._dict)
40+
return super(BaseDict, self).__iter__()
3341

3442
def __len__(self):
35-
return len(self._dict)
36-
37-
def __nonzero__(self):
38-
return bool(self._dict)
43+
if self._dict is not None:
44+
return len(self._dict)
45+
return super(BaseDict, self).__len__()
3946

4047
def __repr__(self):
41-
return repr(self._dict)
48+
if self._dict is not None:
49+
return repr(self._dict)
50+
return super(BaseDict, self).__repr__()
4251

4352
def __setitem__(self, key, value):
44-
self._dict[key] = value
53+
if self._dict is not None:
54+
self._dict[key] = value
55+
return
4556
super(BaseDict, self).__setitem__(key, value)
4657

4758
def __str__(self):
48-
return str(self._dict)
59+
if self._dict is not None:
60+
return str(self._dict)
61+
return super(BaseDict, self).__str__()
4962

5063
def __unicode__(self):
51-
return unicode(self._dict)
64+
if self._dict is not None:
65+
return unicode(self._dict)
66+
return super(BaseDict, self).__unicode__()
5267

5368
def clear(self):
54-
self._dict.clear()
69+
if self._dict is not None:
70+
self._dict.clear()
71+
return
5572
super(BaseDict, self).clear()
5673

5774
def copy(self):
58-
return self._dict.copy()
75+
if self._dict is not None:
76+
return self._dict.copy()
77+
return super(BaseDict, self).copy()
5978

6079
def dict(self):
61-
return self._dict
80+
if self._dict is not None:
81+
return self._dict
82+
return self
6283

6384
def get(self, key, default=None):
64-
return self._dict.get(key, default)
85+
if self._dict is not None:
86+
return self._dict.get(key, default)
87+
return super(BaseDict, self).get(key, default)
6588

6689
def items(self):
67-
return self._dict.items()
90+
if self._dict is not None:
91+
return self._dict.items()
92+
return super(BaseDict, self).items()
6893

6994
def keys(self):
70-
return self._dict.keys()
95+
if self._dict is not None:
96+
return self._dict.keys()
97+
return super(BaseDict, self).keys()
7198

7299
def pop(self, key, *args):
73-
value = self._dict.pop(key, *args)
74-
super(BaseDict, self).pop(key, None)
75-
return value
100+
if self._dict is not None:
101+
return self._dict.pop(key, *args)
102+
return super(BaseDict, self).pop(key, *args)
76103

77104
def setdefault(self, key, default=None):
78-
value = self._dict.setdefault(key, default)
79-
super(BaseDict, self).setdefault(key, default)
80-
return value
105+
if self._dict is not None:
106+
return self._dict.setdefault(key, default)
107+
return super(BaseDict, self).setdefault(key, default)
81108

82109
def update(self, other):
83-
self._dict.update(other)
110+
if self._dict is not None:
111+
self._dict.update(other)
112+
return
84113
super(BaseDict, self).update(other)
85114

86115
def values(self):
87-
return self._dict.values()
116+
if self._dict is not None:
117+
return self._dict.values()
118+
return super(BaseDict, self).values()

0 commit comments

Comments
 (0)