blob: b44598570790f42374623b357780bd6b131b4089 [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
Raymond Hettinger882a4162008-02-07 03:25:46 +00003import collections
4
5class UserList(collections.MutableSequence):
Jeremy Hylton6a973c72000-03-31 00:17:46 +00006 def __init__(self, initlist=None):
Guido van Rossum2a340b31999-03-26 16:20:18 +00007 self.data = []
Jeremy Hylton6a973c72000-03-31 00:17:46 +00008 if initlist is not None:
Thomas Wouters7e474022000-07-16 12:04:32 +00009 # XXX should this accept an arbitrary sequence?
Jeremy Hylton6a973c72000-03-31 00:17:46 +000010 if type(initlist) == type(self.data):
11 self.data[:] = initlist
12 elif isinstance(initlist, UserList):
13 self.data[:] = initlist.data[:]
Guido van Rossum2a340b31999-03-26 16:20:18 +000014 else:
Jeremy Hylton6a973c72000-03-31 00:17:46 +000015 self.data = list(initlist)
Guido van Rossum2a340b31999-03-26 16:20:18 +000016 def __repr__(self): return repr(self.data)
Guido van Rossum753a68e2001-01-18 16:09:55 +000017 def __lt__(self, other): return self.data < self.__cast(other)
18 def __le__(self, other): return self.data <= self.__cast(other)
19 def __eq__(self, other): return self.data == self.__cast(other)
20 def __ne__(self, other): return self.data != self.__cast(other)
21 def __gt__(self, other): return self.data > self.__cast(other)
22 def __ge__(self, other): return self.data >= self.__cast(other)
23 def __cast(self, other):
24 if isinstance(other, UserList): return other.data
25 else: return other
Guido van Rossum2a340b31999-03-26 16:20:18 +000026 def __cmp__(self, other):
Martin v. Löwis0163d6d2001-06-09 07:34:05 +000027 return cmp(self.data, self.__cast(other))
Nick Coghlan48361f52008-08-11 15:45:58 +000028 __hash__ = None # Mutable sequence, so not hashable
Tim Peters7b393fc2000-09-19 20:29:03 +000029 def __contains__(self, item): return item in self.data
Guido van Rossum2a340b31999-03-26 16:20:18 +000030 def __len__(self): return len(self.data)
31 def __getitem__(self, i): return self.data[i]
32 def __setitem__(self, i, item): self.data[i] = item
33 def __delitem__(self, i): del self.data[i]
34 def __getslice__(self, i, j):
35 i = max(i, 0); j = max(j, 0)
Fred Drakecc773d32000-10-06 19:26:01 +000036 return self.__class__(self.data[i:j])
Guido van Rossum2a340b31999-03-26 16:20:18 +000037 def __setslice__(self, i, j, other):
38 i = max(i, 0); j = max(j, 0)
39 if isinstance(other, UserList):
40 self.data[i:j] = other.data
41 elif isinstance(other, type(self.data)):
42 self.data[i:j] = other
43 else:
44 self.data[i:j] = list(other)
45 def __delslice__(self, i, j):
46 i = max(i, 0); j = max(j, 0)
47 del self.data[i:j]
48 def __add__(self, other):
49 if isinstance(other, UserList):
50 return self.__class__(self.data + other.data)
51 elif isinstance(other, type(self.data)):
52 return self.__class__(self.data + other)
53 else:
54 return self.__class__(self.data + list(other))
55 def __radd__(self, other):
56 if isinstance(other, UserList):
57 return self.__class__(other.data + self.data)
58 elif isinstance(other, type(self.data)):
59 return self.__class__(other + self.data)
60 else:
61 return self.__class__(list(other) + self.data)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000062 def __iadd__(self, other):
63 if isinstance(other, UserList):
64 self.data += other.data
65 elif isinstance(other, type(self.data)):
66 self.data += other
67 else:
68 self.data += list(other)
69 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000070 def __mul__(self, n):
71 return self.__class__(self.data*n)
72 __rmul__ = __mul__
Thomas Wouters104a7bc2000-08-24 20:14:10 +000073 def __imul__(self, n):
74 self.data *= n
75 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000076 def append(self, item): self.data.append(item)
77 def insert(self, i, item): self.data.insert(i, item)
78 def pop(self, i=-1): return self.data.pop(i)
79 def remove(self, item): self.data.remove(item)
80 def count(self, item): return self.data.count(item)
Raymond Hettingerd05abde2003-06-17 05:05:49 +000081 def index(self, item, *args): return self.data.index(item, *args)
Guido van Rossum2a340b31999-03-26 16:20:18 +000082 def reverse(self): self.data.reverse()
Raymond Hettinger6b59f5f2003-10-16 05:53:16 +000083 def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
Guido van Rossum2a340b31999-03-26 16:20:18 +000084 def extend(self, other):
85 if isinstance(other, UserList):
86 self.data.extend(other.data)
87 else:
88 self.data.extend(other)