blob: aab511915ccc2a707b84fd09d39ecef9531a6e4c [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)
27 userlist = self.__class__()
28 userlist.data[:] = self.data[i:j]
29 return userlist
30 def __setslice__(self, i, j, other):
31 i = max(i, 0); j = max(j, 0)
32 if isinstance(other, UserList):
33 self.data[i:j] = other.data
34 elif isinstance(other, type(self.data)):
35 self.data[i:j] = other
36 else:
37 self.data[i:j] = list(other)
38 def __delslice__(self, i, j):
39 i = max(i, 0); j = max(j, 0)
40 del self.data[i:j]
41 def __add__(self, other):
42 if isinstance(other, UserList):
43 return self.__class__(self.data + other.data)
44 elif isinstance(other, type(self.data)):
45 return self.__class__(self.data + other)
46 else:
47 return self.__class__(self.data + list(other))
48 def __radd__(self, other):
49 if isinstance(other, UserList):
50 return self.__class__(other.data + self.data)
51 elif isinstance(other, type(self.data)):
52 return self.__class__(other + self.data)
53 else:
54 return self.__class__(list(other) + self.data)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000055 def __iadd__(self, other):
56 if isinstance(other, UserList):
57 self.data += other.data
58 elif isinstance(other, type(self.data)):
59 self.data += other
60 else:
61 self.data += list(other)
62 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000063 def __mul__(self, n):
64 return self.__class__(self.data*n)
65 __rmul__ = __mul__
Thomas Wouters104a7bc2000-08-24 20:14:10 +000066 def __imul__(self, n):
67 self.data *= n
68 return self
Guido van Rossum2a340b31999-03-26 16:20:18 +000069 def append(self, item): self.data.append(item)
70 def insert(self, i, item): self.data.insert(i, item)
71 def pop(self, i=-1): return self.data.pop(i)
72 def remove(self, item): self.data.remove(item)
73 def count(self, item): return self.data.count(item)
74 def index(self, item): return self.data.index(item)
75 def reverse(self): self.data.reverse()
76 def sort(self, *args): apply(self.data.sort, args)
77 def extend(self, other):
78 if isinstance(other, UserList):
79 self.data.extend(other.data)
80 else:
81 self.data.extend(other)