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