blob: 7bd02986dcc394a5b74ad8510ef7a72745547312 [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:
7 # XXX should this accept an arbitary sequence?
8 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)
54 def __mul__(self, n):
55 return self.__class__(self.data*n)
56 __rmul__ = __mul__
57 def append(self, item): self.data.append(item)
58 def insert(self, i, item): self.data.insert(i, item)
59 def pop(self, i=-1): return self.data.pop(i)
60 def remove(self, item): self.data.remove(item)
61 def count(self, item): return self.data.count(item)
62 def index(self, item): return self.data.index(item)
63 def reverse(self): self.data.reverse()
64 def sort(self, *args): apply(self.data.sort, args)
65 def extend(self, other):
66 if isinstance(other, UserList):
67 self.data.extend(other.data)
68 else:
69 self.data.extend(other)