-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistent_list.py
144 lines (110 loc) · 3.83 KB
/
persistent_list.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/durus/persistent_list.py $
$Id: persistent_list.py 31433 2009-02-02 15:10:18Z dbinger $
"""
from durus.persistent import PersistentObject
class PersistentList (PersistentObject):
"""
Instance attributes:
data : list
"""
__slots__ = ['data']
data_is = list # for type checking using QP's spec module
def __init__(self, *args, **kwargs):
self.data = list(*args, **kwargs)
def __cast(self, other):
if isinstance(other, PersistentList): return other.data
else: return other
def __lt__(self, other):
return self is not other and self.data < self.__cast(other)
def __le__(self, other):
return self is other or self.data <= self.__cast(other)
def __eq__(self, other):
return self is other or self.data == self.__cast(other)
def __ne__(self, other):
return self is not other and self.data != self.__cast(other)
def __gt__(self, other):
return self is not other and self.data > self.__cast(other)
def __ge__(self, other):
return self is other or self.data >= self.__cast(other)
def __contains__(self, item):
return item in self.data
def __len__(self):
return len(self.data)
def __getitem__(self, i):
return self.data[i]
def __setitem__(self, i, item):
self._p_note_change()
self.data[i] = item
def __delitem__(self, i):
self._p_note_change()
del self.data[i]
def __getslice__(self, i, j):
return self.__class__(self.data[i:j])
def __setslice__(self, i, j, other):
self._p_note_change()
if isinstance(other, PersistentList):
self.data[i:j] = other.data
elif isinstance(other, type(self.data)):
self.data[i:j] = other
else:
self.data[i:j] = list(other)
def __delslice__(self, i, j):
self._p_note_change()
del self.data[i:j]
def __add__(self, other):
if isinstance(other, PersistentList):
return self.__class__(self.data + other.data)
elif isinstance(other, type(self.data)):
return self.__class__(self.data + other)
else:
return self.__class__(self.data + list(other))
def __radd__(self, other):
if isinstance(other, PersistentList):
return self.__class__(other.data + self.data)
elif isinstance(other, type(self.data)):
return self.__class__(other + self.data)
else:
return self.__class__(list(other) + self.data)
def __iadd__(self, other):
self._p_note_change()
if isinstance(other, PersistentList):
self.data += other.data
else:
self.data += list(other)
return self
def __mul__(self, n):
return self.__class__(self.data * n)
__rmul__ = __mul__
def __imul__(self, n):
self._p_note_change()
self.data *= n
return self
def append(self, item):
self._p_note_change()
self.data.append(item)
def insert(self, i, item):
self._p_note_change()
self.data.insert(i, item)
def pop(self, i=-1):
self._p_note_change()
return self.data.pop(i)
def remove(self, item):
self._p_note_change()
self.data.remove(item)
def count(self, item):
return self.data.count(item)
def index(self, item, *args):
return self.data.index(item, *args)
def reverse(self):
self._p_note_change()
self.data.reverse()
def sort(self, *args, **kwargs):
self._p_note_change()
self.data.sort(*args, **kwargs)
def extend(self, other):
self._p_note_change()
if isinstance(other, PersistentList):
self.data.extend(other.data)
else:
self.data.extend(other)