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