Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """A more or less complete user-defined wrapper around list objects.""" |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 2 | |
| 3 | class UserList: |
Jeremy Hylton | 6a973c7 | 2000-03-31 00:17:46 +0000 | [diff] [blame] | 4 | def __init__(self, initlist=None): |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 5 | self.data = [] |
Jeremy Hylton | 6a973c7 | 2000-03-31 00:17:46 +0000 | [diff] [blame] | 6 | if initlist is not None: |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7 | # XXX should this accept an arbitrary sequence? |
Jeremy Hylton | 6a973c7 | 2000-03-31 00:17:46 +0000 | [diff] [blame] | 8 | if type(initlist) == type(self.data): |
| 9 | self.data[:] = initlist |
| 10 | elif isinstance(initlist, UserList): |
| 11 | self.data[:] = initlist.data[:] |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 12 | else: |
Jeremy Hylton | 6a973c7 | 2000-03-31 00:17:46 +0000 | [diff] [blame] | 13 | self.data = list(initlist) |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 14 | def __repr__(self): return repr(self.data) |
Guido van Rossum | 753a68e | 2001-01-18 16:09:55 +0000 | [diff] [blame] | 15 | def __lt__(self, other): return self.data < self.__cast(other) |
| 16 | def __le__(self, other): return self.data <= self.__cast(other) |
| 17 | def __eq__(self, other): return self.data == self.__cast(other) |
| 18 | def __ne__(self, other): return self.data != self.__cast(other) |
| 19 | def __gt__(self, other): return self.data > self.__cast(other) |
| 20 | def __ge__(self, other): return self.data >= self.__cast(other) |
| 21 | def __cast(self, other): |
| 22 | if isinstance(other, UserList): return other.data |
| 23 | else: return other |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 24 | def __cmp__(self, other): |
Guido van Rossum | 753a68e | 2001-01-18 16:09:55 +0000 | [diff] [blame] | 25 | raise RuntimeError, "UserList.__cmp__() is obsolete" |
Tim Peters | 7b393fc | 2000-09-19 20:29:03 +0000 | [diff] [blame] | 26 | def __contains__(self, item): return item in self.data |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 27 | def __len__(self): return len(self.data) |
| 28 | def __getitem__(self, i): return self.data[i] |
| 29 | def __setitem__(self, i, item): self.data[i] = item |
| 30 | def __delitem__(self, i): del self.data[i] |
| 31 | def __getslice__(self, i, j): |
| 32 | i = max(i, 0); j = max(j, 0) |
Fred Drake | cc773d3 | 2000-10-06 19:26:01 +0000 | [diff] [blame] | 33 | return self.__class__(self.data[i:j]) |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 34 | def __setslice__(self, i, j, other): |
| 35 | i = max(i, 0); j = max(j, 0) |
| 36 | if isinstance(other, UserList): |
| 37 | self.data[i:j] = other.data |
| 38 | elif isinstance(other, type(self.data)): |
| 39 | self.data[i:j] = other |
| 40 | else: |
| 41 | self.data[i:j] = list(other) |
| 42 | def __delslice__(self, i, j): |
| 43 | i = max(i, 0); j = max(j, 0) |
| 44 | del self.data[i:j] |
| 45 | def __add__(self, other): |
| 46 | if isinstance(other, UserList): |
| 47 | return self.__class__(self.data + other.data) |
| 48 | elif isinstance(other, type(self.data)): |
| 49 | return self.__class__(self.data + other) |
| 50 | else: |
| 51 | return self.__class__(self.data + list(other)) |
| 52 | def __radd__(self, other): |
| 53 | if isinstance(other, UserList): |
| 54 | return self.__class__(other.data + self.data) |
| 55 | elif isinstance(other, type(self.data)): |
| 56 | return self.__class__(other + self.data) |
| 57 | else: |
| 58 | return self.__class__(list(other) + self.data) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 59 | def __iadd__(self, other): |
| 60 | if isinstance(other, UserList): |
| 61 | self.data += other.data |
| 62 | elif isinstance(other, type(self.data)): |
| 63 | self.data += other |
| 64 | else: |
| 65 | self.data += list(other) |
| 66 | return self |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 67 | def __mul__(self, n): |
| 68 | return self.__class__(self.data*n) |
| 69 | __rmul__ = __mul__ |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 70 | def __imul__(self, n): |
| 71 | self.data *= n |
| 72 | return self |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 73 | def append(self, item): self.data.append(item) |
| 74 | def insert(self, i, item): self.data.insert(i, item) |
| 75 | def pop(self, i=-1): return self.data.pop(i) |
| 76 | def remove(self, item): self.data.remove(item) |
| 77 | def count(self, item): return self.data.count(item) |
| 78 | def index(self, item): return self.data.index(item) |
| 79 | def reverse(self): self.data.reverse() |
| 80 | def sort(self, *args): apply(self.data.sort, args) |
| 81 | def extend(self, other): |
| 82 | if isinstance(other, UserList): |
| 83 | self.data.extend(other.data) |
| 84 | else: |
| 85 | self.data.extend(other) |