-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent_dict.py
123 lines (95 loc) · 3.03 KB
/
persistent_dict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/durus/persistent_dict.py $
$Id: persistent_dict.py 30862 2008-06-18 14:13:35Z dbinger $
"""
from copy import copy
from durus.persistent import PersistentObject
from durus.utils import iteritems
class PersistentDict (PersistentObject):
"""
Instance attributes:
data : dict
"""
__slots__ = ['data']
data_is = dict # for type checking using QP's spec module
def __init__(self, *args, **kwargs):
self.data = dict(*args, **kwargs)
def __eq__(self, other):
return isinstance(other, PersistentDict) and self.data == other.data
def __ne__(self, other):
return not self == other
def __len__(self):
return len(self.data)
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, item):
self._p_note_change()
self.data[key] = item
def __delitem__(self, key):
self._p_note_change()
del self.data[key]
def clear(self):
self._p_note_change()
self.data.clear()
def copy(self):
result = copy(self)
result.data = self.data.copy()
return result
def keys(self):
return list(self.data.keys())
def items(self):
return list(self.data.items())
def iteritems(self):
return iteritems(self.data)
def iterkeys(self):
for k, v in self.iteritems():
yield k
def itervalues(self):
for k, v in self.iteritems():
yield v
def values(self):
return list(self.data.values())
def has_key(self, key):
return key in self.data
def update(self, *others, **kwargs):
self._p_note_change()
if len(others) > 1:
raise TypeError("update() expected at most 1 argument")
elif others:
other = others[0]
if isinstance(other, PersistentDict):
self.data.update(other.data)
elif isinstance(other, dict):
self.data.update(other)
elif hasattr(other, 'keys'):
for k in other.keys():
self[k] = other[k]
else:
for k, v in other:
self[k] = v
for kw in kwargs:
self[kw] = kwargs[kw]
def get(self, key, failobj=None):
return self.data.get(key, failobj)
def setdefault(self, key, failobj=None):
if key not in self.data:
self._p_note_change()
self.data[key] = failobj
return failobj
return self.data[key]
def pop(self, key, *args):
self._p_note_change()
return self.data.pop(key, *args)
def popitem(self):
self._p_note_change()
return self.data.popitem()
def __contains__(self, key):
return key in self.data
def fromkeys(cls, iterable, value=None):
d = cls()
for key in iterable:
d[key] = value
return d
fromkeys = classmethod(fromkeys)
def __iter__(self):
return iter(self.data)