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