blob: 116122f6238dc70036f77ffec33fe9b169e07169 [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)
Guido van Rossum753a68e2001-01-18 16:09:55 +000015 def __lt__(self, other): return self.data < self.__cast(other)
16 def __le__(self, other): return self.data <= self.__cast(other)
17 def __eq__(self, other): return self.data == self.__cast(other)
18 def __ne__(self, other): return self.data != self.__cast(other)
19 def __gt__(self, other): return self.data > self.__cast(other)
20 def __ge__(self, other): return self.data >= self.__cast(other)
21 def __cast(self, other):
22 if isinstance(other, UserList): return other.data
23 else: return other
Guido van Rossum2a340b31999-03-26 16:20:18 +000024 def __cmp__(self, other):
Martin v. Löwis0163d6d2001-06-09 07:34:05 +000025 return cmp(self.data, self.__cast(other))
Tim Peters7b393fc2000-09-19 20:29:03 +000026 def __contains__(self, item): return item in self.data
Guido van Rossum2a340b31999-03-26 16:20:18 +000027 def __len__(self): return len(self.data)
28 def __getitem__(self, i): return self.data[i]
29 def __setitem__(self, i, item): self.data[i] = item
30 def __delitem__(self, i): del self.data[i]
Guido van Rossum2a340b31999-03-26 16:20:18 +000031 def __add__(self, other):
32 if isinstance(other, UserList):
33 return self.__class__(self.data + other.data)
34 elif isinstance(other, type(self.data)):
35 return self.__class__(self.data + other)
36 else:
37 return self.__class__(self.data + list(other))
38 def __radd__(self, other):
39 if isinstance(other, UserList):
40 return self.__class__(other.data + self.data)
41 elif isinstance(other, type(self.data)):
42 return self.__class__(other + self.data)
43 else:
44 return self.__class__(list(other) + self.data)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000045 def __iadd__(self, other):
46 if isinstance(other, UserList):
47 self.data += other.data
48 elif isinstance(other, type(self.data)):
49 self.data += other
50 else:
51 self.data += list(other)
52 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000053 def __mul__(self, n):
54 return self.__class__(self.data*n)
55 __rmul__ = __mul__
Thomas Wouters104a7bc2000-08-24 20:14:10 +000056 def __imul__(self, n):
57 self.data *= n
58 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000059 def append(self, item): self.data.append(item)
60 def insert(self, i, item): self.data.insert(i, item)
61 def pop(self, i=-1): return self.data.pop(i)
62 def remove(self, item): self.data.remove(item)
63 def count(self, item): return self.data.count(item)
Raymond Hettingerd05abde2003-06-17 05:05:49 +000064 def index(self, item, *args): return self.data.index(item, *args)
Guido van Rossum2a340b31999-03-26 16:20:18 +000065 def reverse(self): self.data.reverse()
Raymond Hettinger6b59f5f2003-10-16 05:53:16 +000066 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
Guido van Rossum2a340b31999-03-26 16:20:18 +000067 def extend(self, other):
68 if isinstance(other, UserList):
69 self.data.extend(other.data)
70 else:
71 self.data.extend(other)