2000-02-02 11:10:15 -04:00
|
|
|
"""A more or less complete user-defined wrapper around list objects."""
|
1993-11-30 09:43:54 -04:00
|
|
|
|
|
|
|
class UserList:
|
2000-03-30 20:17:46 -04:00
|
|
|
def __init__(self, initlist=None):
|
1999-03-26 12:20:18 -04:00
|
|
|
self.data = []
|
2000-03-30 20:17:46 -04:00
|
|
|
if initlist is not None:
|
2000-07-16 09:04:32 -03:00
|
|
|
# XXX should this accept an arbitrary sequence?
|
2000-03-30 20:17:46 -04:00
|
|
|
if type(initlist) == type(self.data):
|
|
|
|
self.data[:] = initlist
|
|
|
|
elif isinstance(initlist, UserList):
|
|
|
|
self.data[:] = initlist.data[:]
|
1999-03-26 12:20:18 -04:00
|
|
|
else:
|
2000-03-30 20:17:46 -04:00
|
|
|
self.data = list(initlist)
|
1999-03-26 12:20:18 -04:00
|
|
|
def __repr__(self): return repr(self.data)
|
2001-01-18 12:09:55 -04:00
|
|
|
def __lt__(self, other): return self.data < self.__cast(other)
|
|
|
|
def __le__(self, other): return self.data <= self.__cast(other)
|
|
|
|
def __eq__(self, other): return self.data == self.__cast(other)
|
|
|
|
def __ne__(self, other): return self.data != self.__cast(other)
|
|
|
|
def __gt__(self, other): return self.data > self.__cast(other)
|
|
|
|
def __ge__(self, other): return self.data >= self.__cast(other)
|
|
|
|
def __cast(self, other):
|
|
|
|
if isinstance(other, UserList): return other.data
|
|
|
|
else: return other
|
1999-03-26 12:20:18 -04:00
|
|
|
def __cmp__(self, other):
|
2001-06-09 04:34:05 -03:00
|
|
|
return cmp(self.data, self.__cast(other))
|
2000-09-19 17:29:03 -03:00
|
|
|
def __contains__(self, item): return item in self.data
|
1999-03-26 12:20:18 -04:00
|
|
|
def __len__(self): return len(self.data)
|
|
|
|
def __getitem__(self, i): return self.data[i]
|
|
|
|
def __setitem__(self, i, item): self.data[i] = item
|
|
|
|
def __delitem__(self, i): del self.data[i]
|
|
|
|
def __getslice__(self, i, j):
|
|
|
|
i = max(i, 0); j = max(j, 0)
|
2000-10-06 16:26:01 -03:00
|
|
|
return self.__class__(self.data[i:j])
|
1999-03-26 12:20:18 -04:00
|
|
|
def __setslice__(self, i, j, other):
|
|
|
|
i = max(i, 0); j = max(j, 0)
|
|
|
|
if isinstance(other, UserList):
|
|
|
|
self.data[i:j] = other.data
|
|
|
|
elif isinstance(other, type(self.data)):
|
|
|
|
self.data[i:j] = other
|
|
|
|
else:
|
|
|
|
self.data[i:j] = list(other)
|
|
|
|
def __delslice__(self, i, j):
|
|
|
|
i = max(i, 0); j = max(j, 0)
|
|
|
|
del self.data[i:j]
|
|
|
|
def __add__(self, other):
|
|
|
|
if isinstance(other, UserList):
|
|
|
|
return self.__class__(self.data + other.data)
|
|
|
|
elif isinstance(other, type(self.data)):
|
|
|
|
return self.__class__(self.data + other)
|
|
|
|
else:
|
|
|
|
return self.__class__(self.data + list(other))
|
|
|
|
def __radd__(self, other):
|
|
|
|
if isinstance(other, UserList):
|
|
|
|
return self.__class__(other.data + self.data)
|
|
|
|
elif isinstance(other, type(self.data)):
|
|
|
|
return self.__class__(other + self.data)
|
|
|
|
else:
|
|
|
|
return self.__class__(list(other) + self.data)
|
2000-08-24 17:14:10 -03:00
|
|
|
def __iadd__(self, other):
|
|
|
|
if isinstance(other, UserList):
|
|
|
|
self.data += other.data
|
|
|
|
elif isinstance(other, type(self.data)):
|
|
|
|
self.data += other
|
|
|
|
else:
|
|
|
|
self.data += list(other)
|
|
|
|
return self
|
1999-03-26 12:20:18 -04:00
|
|
|
def __mul__(self, n):
|
|
|
|
return self.__class__(self.data*n)
|
|
|
|
__rmul__ = __mul__
|
2000-08-24 17:14:10 -03:00
|
|
|
def __imul__(self, n):
|
|
|
|
self.data *= n
|
|
|
|
return self
|
1999-03-26 12:20:18 -04:00
|
|
|
def append(self, item): self.data.append(item)
|
|
|
|
def insert(self, i, item): self.data.insert(i, item)
|
|
|
|
def pop(self, i=-1): return self.data.pop(i)
|
|
|
|
def remove(self, item): self.data.remove(item)
|
|
|
|
def count(self, item): return self.data.count(item)
|
|
|
|
def index(self, item): return self.data.index(item)
|
|
|
|
def reverse(self): self.data.reverse()
|
|
|
|
def sort(self, *args): apply(self.data.sort, args)
|
|
|
|
def extend(self, other):
|
|
|
|
if isinstance(other, UserList):
|
|
|
|
self.data.extend(other.data)
|
|
|
|
else:
|
|
|
|
self.data.extend(other)
|