blob: e79faea1a1b440b72c36fd3545058ce8d3abcfbe [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)
Tim Peters7b393fc2000-09-19 20:29:03 +000020 def __contains__(self, item): return item in self.data
Guido van Rossum2a340b31999-03-26 16:20:18 +000021 def __len__(self): return len(self.data)
22 def __getitem__(self, i): return self.data[i]
23 def __setitem__(self, i, item): self.data[i] = item
24 def __delitem__(self, i): del self.data[i]
25 def __getslice__(self, i, j):
26 i = max(i, 0); j = max(j, 0)
Fred Drakecc773d32000-10-06 19:26:01 +000027 return self.__class__(self.data[i:j])
Guido van Rossum2a340b31999-03-26 16:20:18 +000028 def __setslice__(self, i, j, other):
29 i = max(i, 0); j = max(j, 0)
30 if isinstance(other, UserList):
31 self.data[i:j] = other.data
32 elif isinstance(other, type(self.data)):
33 self.data[i:j] = other
34 else:
35 self.data[i:j] = list(other)
36 def __delslice__(self, i, j):
37 i = max(i, 0); j = max(j, 0)
38 del self.data[i:j]
39 def __add__(self, other):
40 if isinstance(other, UserList):
41 return self.__class__(self.data + other.data)
42 elif isinstance(other, type(self.data)):
43 return self.__class__(self.data + other)
44 else:
45 return self.__class__(self.data + list(other))
46 def __radd__(self, other):
47 if isinstance(other, UserList):
48 return self.__class__(other.data + self.data)
49 elif isinstance(other, type(self.data)):
50 return self.__class__(other + self.data)
51 else:
52 return self.__class__(list(other) + self.data)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000053 def __iadd__(self, other):
54 if isinstance(other, UserList):
55 self.data += other.data
56 elif isinstance(other, type(self.data)):
57 self.data += other
58 else:
59 self.data += list(other)
60 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000061 def __mul__(self, n):
62 return self.__class__(self.data*n)
63 __rmul__ = __mul__
Thomas Wouters104a7bc2000-08-24 20:14:10 +000064 def __imul__(self, n):
65 self.data *= n
66 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000067 def append(self, item): self.data.append(item)
68 def insert(self, i, item): self.data.insert(i, item)
69 def pop(self, i=-1): return self.data.pop(i)
70 def remove(self, item): self.data.remove(item)
71 def count(self, item): return self.data.count(item)
72 def index(self, item): return self.data.index(item)
73 def reverse(self): self.data.reverse()
74 def sort(self, *args): apply(self.data.sort, args)
75 def extend(self, other):
76 if isinstance(other, UserList):
77 self.data.extend(other.data)
78 else:
79 self.data.extend(other)