| 1 | 1 | """A more or less complete user-defined wrapper around list objects.""" |
|---|
| 2 | n/a | |
|---|
| 3 | 1 | import collections |
|---|
| 4 | n/a | |
|---|
| 5 | 2 | class UserList(collections.MutableSequence): |
|---|
| 6 | 1 | def __init__(self, initlist=None): |
|---|
| 7 | 900 | self.data = [] |
|---|
| 8 | 900 | if initlist is not None: |
|---|
| 9 | n/a | # XXX should this accept an arbitrary sequence? |
|---|
| 10 | 872 | if type(initlist) == type(self.data): |
|---|
| 11 | 702 | self.data[:] = initlist |
|---|
| 12 | 170 | elif isinstance(initlist, UserList): |
|---|
| 13 | 7 | self.data[:] = initlist.data[:] |
|---|
| 14 | n/a | else: |
|---|
| 15 | 163 | self.data = list(initlist) |
|---|
| 16 | 16 | def __repr__(self): return repr(self.data) |
|---|
| 17 | 1 | def __lt__(self, other): return self.data < self.__cast(other) |
|---|
| 18 | 1 | def __le__(self, other): return self.data <= self.__cast(other) |
|---|
| 19 | 818 | def __eq__(self, other): return self.data == self.__cast(other) |
|---|
| 20 | 3 | def __ne__(self, other): return self.data != self.__cast(other) |
|---|
| 21 | 1 | def __gt__(self, other): return self.data > self.__cast(other) |
|---|
| 22 | 1 | def __ge__(self, other): return self.data >= self.__cast(other) |
|---|
| 23 | 1 | def __cast(self, other): |
|---|
| 24 | 819 | if isinstance(other, UserList): return other.data |
|---|
| 25 | 405 | else: return other |
|---|
| 26 | 1 | def __cmp__(self, other): |
|---|
| 27 | 1 | return cmp(self.data, self.__cast(other)) |
|---|
| 28 | 1 | __hash__ = None # Mutable sequence, so not hashable |
|---|
| 29 | 11 | def __contains__(self, item): return item in self.data |
|---|
| 30 | 1400 | def __len__(self): return len(self.data) |
|---|
| 31 | 9193 | def __getitem__(self, i): return self.data[i] |
|---|
| 32 | 30 | def __setitem__(self, i, item): self.data[i] = item |
|---|
| 33 | 13 | def __delitem__(self, i): del self.data[i] |
|---|
| 34 | 1 | def __getslice__(self, i, j): |
|---|
| 35 | 247 | i = max(i, 0); j = max(j, 0) |
|---|
| 36 | 247 | return self.__class__(self.data[i:j]) |
|---|
| 37 | 1 | def __setslice__(self, i, j, other): |
|---|
| 38 | 137 | i = max(i, 0); j = max(j, 0) |
|---|
| 39 | 137 | if isinstance(other, UserList): |
|---|
| 40 | 67 | self.data[i:j] = other.data |
|---|
| 41 | 70 | elif isinstance(other, type(self.data)): |
|---|
| 42 | 67 | self.data[i:j] = other |
|---|
| 43 | n/a | else: |
|---|
| 44 | 3 | self.data[i:j] = list(other) |
|---|
| 45 | 1 | def __delslice__(self, i, j): |
|---|
| 46 | 14 | i = max(i, 0); j = max(j, 0) |
|---|
| 47 | 14 | del self.data[i:j] |
|---|
| 48 | 1 | def __add__(self, other): |
|---|
| 49 | 17 | if isinstance(other, UserList): |
|---|
| 50 | 14 | return self.__class__(self.data + other.data) |
|---|
| 51 | 3 | elif isinstance(other, type(self.data)): |
|---|
| 52 | 2 | return self.__class__(self.data + other) |
|---|
| 53 | n/a | else: |
|---|
| 54 | 1 | return self.__class__(self.data + list(other)) |
|---|
| 55 | 1 | def __radd__(self, other): |
|---|
| 56 | 3 | if isinstance(other, UserList): |
|---|
| 57 | 1 | return self.__class__(other.data + self.data) |
|---|
| 58 | 2 | elif isinstance(other, type(self.data)): |
|---|
| 59 | 1 | return self.__class__(other + self.data) |
|---|
| 60 | n/a | else: |
|---|
| 61 | 1 | return self.__class__(list(other) + self.data) |
|---|
| 62 | 1 | def __iadd__(self, other): |
|---|
| 63 | 7 | if isinstance(other, UserList): |
|---|
| 64 | 4 | self.data += other.data |
|---|
| 65 | 3 | elif isinstance(other, type(self.data)): |
|---|
| 66 | 1 | self.data += other |
|---|
| 67 | n/a | else: |
|---|
| 68 | 2 | self.data += list(other) |
|---|
| 69 | 6 | return self |
|---|
| 70 | 1 | def __mul__(self, n): |
|---|
| 71 | 53 | return self.__class__(self.data*n) |
|---|
| 72 | 1 | __rmul__ = __mul__ |
|---|
| 73 | 1 | def __imul__(self, n): |
|---|
| 74 | 3 | self.data *= n |
|---|
| 75 | 3 | return self |
|---|
| 76 | 43 | def append(self, item): self.data.append(item) |
|---|
| 77 | 1007 | def insert(self, i, item): self.data.insert(i, item) |
|---|
| 78 | 6 | def pop(self, i=-1): return self.data.pop(i) |
|---|
| 79 | 11 | def remove(self, item): self.data.remove(item) |
|---|
| 80 | 6 | def count(self, item): return self.data.count(item) |
|---|
| 81 | 24 | def index(self, item, *args): return self.data.index(item, *args) |
|---|
| 82 | 3 | def reverse(self): self.data.reverse() |
|---|
| 83 | 10 | def sort(self, *args, **kwds): self.data.sort(*args, **kwds) |
|---|
| 84 | 1 | def extend(self, other): |
|---|
| 85 | 6 | if isinstance(other, UserList): |
|---|
| 86 | 3 | self.data.extend(other.data) |
|---|
| 87 | n/a | else: |
|---|
| 88 | 3 | self.data.extend(other) |
|---|