|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class BaseDict(dict): |
|
5
|
|
|
|
|
6
|
|
|
_dict = {} |
|
7
|
|
|
|
|
8
|
|
|
def __init__(self, *args, **kwargs): |
|
9
|
|
|
if len(args) == 1 and isinstance(args[0], dict): |
|
10
|
|
|
self._dict = args[0] |
|
11
|
|
|
else: |
|
12
|
|
|
self._dict = dict(*args, **kwargs) |
|
13
|
|
|
super(BaseDict, self).__init__(*args, **kwargs) |
|
14
|
|
|
|
|
15
|
|
|
def __bool__(self): |
|
16
|
|
|
return bool(self._dict) |
|
17
|
|
|
|
|
18
|
|
|
def __contains__(self, key): |
|
19
|
|
|
return key in self._dict |
|
20
|
|
|
|
|
21
|
|
|
def __delitem__(self, key): |
|
22
|
|
|
del self._dict[key] |
|
23
|
|
|
super(BaseDict, self).__delitem__(key) |
|
24
|
|
|
|
|
25
|
|
|
def __eq__(self, other): |
|
26
|
|
|
return self._dict == other |
|
27
|
|
|
|
|
28
|
|
|
def __getitem__(self, key): |
|
29
|
|
|
return self._dict[key] |
|
30
|
|
|
|
|
31
|
|
|
def __iter__(self): |
|
32
|
|
|
return iter(self._dict) |
|
33
|
|
|
|
|
34
|
|
|
def __len__(self): |
|
35
|
|
|
return len(self._dict) |
|
36
|
|
|
|
|
37
|
|
|
def __nonzero__(self): |
|
38
|
|
|
return bool(self._dict) |
|
39
|
|
|
|
|
40
|
|
|
def __repr__(self): |
|
41
|
|
|
return repr(self._dict) |
|
42
|
|
|
|
|
43
|
|
|
def __setitem__(self, key, value): |
|
44
|
|
|
self._dict[key] = value |
|
45
|
|
|
super(BaseDict, self).__setitem__(key, value) |
|
46
|
|
|
|
|
47
|
|
|
def __str__(self): |
|
48
|
|
|
return str(self._dict) |
|
49
|
|
|
|
|
50
|
|
|
def __unicode__(self): |
|
51
|
|
|
return unicode(self._dict) |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def clear(self): |
|
54
|
|
|
self._dict.clear() |
|
55
|
|
|
super(BaseDict, self).clear() |
|
56
|
|
|
|
|
57
|
|
|
def copy(self): |
|
58
|
|
|
return self._dict.copy() |
|
59
|
|
|
|
|
60
|
|
|
def dict(self): |
|
61
|
|
|
return self._dict |
|
62
|
|
|
|
|
63
|
|
|
def get(self, key, default=None): |
|
64
|
|
|
return self._dict.get(key, default) |
|
65
|
|
|
|
|
66
|
|
|
def items(self): |
|
67
|
|
|
return self._dict.items() |
|
68
|
|
|
|
|
69
|
|
|
def keys(self): |
|
70
|
|
|
return self._dict.keys() |
|
71
|
|
|
|
|
72
|
|
|
def pop(self, key, *args): |
|
73
|
|
|
value = self._dict.pop(key, *args) |
|
74
|
|
|
super(BaseDict, self).pop(key, None) |
|
75
|
|
|
return value |
|
76
|
|
|
|
|
77
|
|
|
def setdefault(self, key, default=None): |
|
78
|
|
|
value = self._dict.setdefault(key, default) |
|
79
|
|
|
super(BaseDict, self).setdefault(key, default) |
|
80
|
|
|
return value |
|
81
|
|
|
|
|
82
|
|
|
def update(self, other): |
|
83
|
|
|
self._dict.update(other) |
|
84
|
|
|
super(BaseDict, self).update(other) |
|
85
|
|
|
|
|
86
|
|
|
def values(self): |
|
87
|
|
|
return self._dict.values() |
|
88
|
|
|
|