1
2
3 __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
4 __version__ = "0.2"
5 __license__ = "GNU Lesser General Public License"
6
7 import sys
9 - def __init__(self, lst, add_func, remove_func):
10 self._list = lst
11 self._add_func = add_func
12 self._remove_func = remove_func
13
15 if not isinstance(lst, (list, tuple)):
16 lst = [lst]
17 self._add_func(lst)
18
20 if not isinstance(lst, (list, tuple)):
21 lst = [lst]
22 for l in lst:
23 self._remove_func(l)
24
26 for i in xrange(len(self._list)):
27 yield self._list[i]
28
30 """
31 Get (start, end, step) tuple from slice object.
32 """
33 (start, end, step) = i.indices(len(self._list))
34
35 if step == 1:
36 if end < start:
37 end = start
38 step = None
39 if i.step == None:
40 step = None
41 return (start, end, step)
42
43
45 if isinstance(i, slice):
46 (start, end, step) = self._tuple_from_slice(i)
47 if step == None:
48 indices = xrange(start, end)
49 else:
50 indices = xrange(start, end, step)
51 return [self._list[i] for i in indices]
52 else:
53 return self._list[i]
54
55 - def index(self, x, i=0, j=None):
56 if i != 0 or j is not None:
57 (i, j, ignore) = self._tuple_from_slice(slice(i, j))
58 if j is None:
59 j = len(self)
60 for k in xrange(i, j):
61 if self._list[k] == x:
62 return k
63 raise ValueError('index(x): x not in list')
64
65
66 if sys.version_info[:3] < (2, 4, 0):
67 - def sort(self, cmpfunc=None):
68 ans = list(self._list)
69 ans.sort(cmpfunc)
70 self._list[:] = ans
71 else:
72 - def sort(self, cmpfunc=None, key=None, reverse=False):
73 ans = list(self._list)
74 if reverse == True:
75 ans.sort(cmpfunc, key, reverse)
76 elif key != None:
77 ans.sort(cmpfunc, key)
78 else:
79 ans.sort(cmpfunc)
80 self._list[:] = ans
81
83 return len(self._list)
84
86 return repr(self._list)
87