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) |
|
|
|
|
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
|
|
|
|