blob: 114606098ce09337551b9de508af8be2aaad926a [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""A more or less complete user-defined wrapper around list objects."""
Guido van Rossumae3b3a31993-11-30 13:43:54 +00002
3class UserList:
Jeremy Hylton6a973c72000-03-31 00:17:46 +00004 def __init__(self, initlist=None):
Guido van Rossum2a340b31999-03-26 16:20:18 +00005 self.data = []
Jeremy Hylton6a973c72000-03-31 00:17:46 +00006 if initlist is not None:
Thomas Wouters7e474022000-07-16 12:04:32 +00007 # XXX should this accept an arbitrary sequence?
Jeremy Hylton6a973c72000-03-31 00:17:46 +00008 if type(initlist) == type(self.data):
9 self.data[:] = initlist
10 elif isinstance(initlist, UserList):
11 self.data[:] = initlist.data[:]
Guido van Rossum2a340b31999-03-26 16:20:18 +000012 else:
Jeremy Hylton6a973c72000-03-31 00:17:46 +000013 self.data = list(initlist)
Guido van Rossum2a340b31999-03-26 16:20:18 +000014 def __repr__(self): return repr(self.data)
15 def __cmp__(self, other):
16 if isinstance(other, UserList):
17 return cmp(self.data, other.data)
18 else:
19 return cmp(self.data, other)
20 def __len__(self): return len(self.data)
21 def __getitem__(self, i): return self.data[i]
22 def __setitem__(self, i, item): self.data[i] = item
23 def __delitem__(self, i): del self.data[i]
24 def __getslice__(self, i, j):
25 i = max(i, 0); j = max(j, 0)
26 userlist = self.__class__()
27 userlist.data[:] = self.data[i:j]
28 return userlist
29 def __setslice__(self, i, j, other):
30 i = max(i, 0); j = max(j, 0)
31 if isinstance(other, UserList):
32 self.data[i:j] = other.data
33 elif isinstance(other, type(self.data)):
34 self.data[i:j] = other
35 else:
36 self.data[i:j] = list(other)
37 def __delslice__(self, i, j):
38 i = max(i, 0); j = max(j, 0)
39 del self.data[i:j]
40 def __add__(self, other):
41 if isinstance(other, UserList):
42 return self.__class__(self.data + other.data)
43 elif isinstance(other, type(self.data)):
44 return self.__class__(self.data + other)
45 else:
46 return self.__class__(self.data + list(other))
47 def __radd__(self, other):
48 if isinstance(other, UserList):
49 return self.__class__(other.data + self.data)
50 elif isinstance(other, type(self.data)):
51 return self.__class__(other + self.data)
52 else:
53 return self.__class__(list(other) + self.data)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000054 def __iadd__(self, other):
55 if isinstance(other, UserList):
56 self.data += other.data
57 elif isinstance(other, type(self.data)):
58 self.data += other
59 else:
60 self.data += list(other)
61 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000062 def __mul__(self, n):
63 return self.__class__(self.data*n)
64 __rmul__ = __mul__
Thomas Wouters104a7bc2000-08-24 20:14:10 +000065 def __imul__(self, n):
66 self.data *= n
67 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000068 def append(self, item): self.data.append(item)
69 def insert(self, i, item): self.data.insert(i, item)
70 def pop(self, i=-1): return self.data.pop(i)
71 def remove(self, item): self.data.remove(item)
72 def count(self, item): return self.data.count(item)
73 def index(self, item): return self.data.index(item)
74 def reverse(self): self.data.reverse()
75 def sort(self, *args): apply(self.data.sort, args)
76 def extend(self, other):
77 if isinstance(other, UserList):
78 self.data.extend(other.data)
79 else:
80 self.data.extend(other)