diff options
author | Raymond Hettinger <python@rcn.com> | 2008-02-12 20:03:09 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-02-12 20:03:09 (GMT) |
commit | 53dbe39b46ef42aeef12c6f46f8575a794e20440 (patch) | |
tree | 99f15bb4ba51d6e88ad50f462af0ed0235b1d6d5 /Lib/collections.py | |
parent | 4513ef8b7a4a684deea0dda23a760f4596a1097c (diff) | |
download | cpython-53dbe39b46ef42aeef12c6f46f8575a794e20440.zip cpython-53dbe39b46ef42aeef12c6f46f8575a794e20440.tar.gz cpython-53dbe39b46ef42aeef12c6f46f8575a794e20440.tar.bz2 |
Move UserList to collections.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index abde718..93194e0 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -163,6 +163,80 @@ class UserDict(MutableMapping): ################################################################################ +### UserList +################################################################################ + +class UserList(MutableSequence): + """A more or less complete user-defined wrapper around list objects.""" + def __init__(self, initlist=None): + self.data = [] + if initlist is not None: + # XXX should this accept an arbitrary sequence? + if type(initlist) == type(self.data): + self.data[:] = initlist + elif isinstance(initlist, UserList): + self.data[:] = initlist.data[:] + else: + self.data = list(initlist) + def __repr__(self): return repr(self.data) + 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): + return other.data if isinstance(other, UserList) else other + def __cmp__(self, other): + return cmp(self.data, self.__cast(other)) + def __contains__(self, item): return item in self.data + 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 __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) + 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) + return self.__class__(list(other) + self.data) + 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 + def __mul__(self, n): + return self.__class__(self.data*n) + __rmul__ = __mul__ + def __imul__(self, n): + self.data *= n + return self + 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, *args): return self.data.index(item, *args) + def reverse(self): self.data.reverse() + def sort(self, *args, **kwds): self.data.sort(*args, **kwds) + def extend(self, other): + if isinstance(other, UserList): + self.data.extend(other.data) + else: + self.data.extend(other) + + + +################################################################################ ### Simple tests ################################################################################ |