+++ /dev/null
-"""A more or less complete user-defined wrapper around list objects."""\r
-\r
-import collections\r
-\r
-class UserList(collections.MutableSequence):\r
- def __init__(self, initlist=None):\r
- self.data = []\r
- if initlist is not None:\r
- # XXX should this accept an arbitrary sequence?\r
- if type(initlist) == type(self.data):\r
- self.data[:] = initlist\r
- elif isinstance(initlist, UserList):\r
- self.data[:] = initlist.data[:]\r
- else:\r
- self.data = list(initlist)\r
- def __repr__(self): return repr(self.data)\r
- def __lt__(self, other): return self.data < self.__cast(other)\r
- def __le__(self, other): return self.data <= self.__cast(other)\r
- def __eq__(self, other): return self.data == self.__cast(other)\r
- def __ne__(self, other): return self.data != self.__cast(other)\r
- def __gt__(self, other): return self.data > self.__cast(other)\r
- def __ge__(self, other): return self.data >= self.__cast(other)\r
- def __cast(self, other):\r
- if isinstance(other, UserList): return other.data\r
- else: return other\r
- def __cmp__(self, other):\r
- return cmp(self.data, self.__cast(other))\r
- __hash__ = None # Mutable sequence, so not hashable\r
- def __contains__(self, item): return item in self.data\r
- def __len__(self): return len(self.data)\r
- def __getitem__(self, i): return self.data[i]\r
- def __setitem__(self, i, item): self.data[i] = item\r
- def __delitem__(self, i): del self.data[i]\r
- def __getslice__(self, i, j):\r
- i = max(i, 0); j = max(j, 0)\r
- return self.__class__(self.data[i:j])\r
- def __setslice__(self, i, j, other):\r
- i = max(i, 0); j = max(j, 0)\r
- if isinstance(other, UserList):\r
- self.data[i:j] = other.data\r
- elif isinstance(other, type(self.data)):\r
- self.data[i:j] = other\r
- else:\r
- self.data[i:j] = list(other)\r
- def __delslice__(self, i, j):\r
- i = max(i, 0); j = max(j, 0)\r
- del self.data[i:j]\r
- def __add__(self, other):\r
- if isinstance(other, UserList):\r
- return self.__class__(self.data + other.data)\r
- elif isinstance(other, type(self.data)):\r
- return self.__class__(self.data + other)\r
- else:\r
- return self.__class__(self.data + list(other))\r
- def __radd__(self, other):\r
- if isinstance(other, UserList):\r
- return self.__class__(other.data + self.data)\r
- elif isinstance(other, type(self.data)):\r
- return self.__class__(other + self.data)\r
- else:\r
- return self.__class__(list(other) + self.data)\r
- def __iadd__(self, other):\r
- if isinstance(other, UserList):\r
- self.data += other.data\r
- elif isinstance(other, type(self.data)):\r
- self.data += other\r
- else:\r
- self.data += list(other)\r
- return self\r
- def __mul__(self, n):\r
- return self.__class__(self.data*n)\r
- __rmul__ = __mul__\r
- def __imul__(self, n):\r
- self.data *= n\r
- return self\r
- def append(self, item): self.data.append(item)\r
- def insert(self, i, item): self.data.insert(i, item)\r
- def pop(self, i=-1): return self.data.pop(i)\r
- def remove(self, item): self.data.remove(item)\r
- def count(self, item): return self.data.count(item)\r
- def index(self, item, *args): return self.data.index(item, *args)\r
- def reverse(self): self.data.reverse()\r
- def sort(self, *args, **kwds): self.data.sort(*args, **kwds)\r
- def extend(self, other):\r
- if isinstance(other, UserList):\r
- self.data.extend(other.data)\r
- else:\r
- self.data.extend(other)\r