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) |
| 15 | def __cmp__(self, other): |
| 16 | if isinstance(other, UserList): |
| 17 | return cmp(self.data, other.data) |
| 18 | else: |
| 19 | return cmp(self.data, other) |
| 20 | def __len__(self): return len(self.data) |
| 21 | def __getitem__(self, i): return self.data[i] |
| 22 | def __setitem__(self, i, item): self.data[i] = item |
| 23 | def __delitem__(self, i): del self.data[i] |
| 24 | def __getslice__(self, i, j): |
| 25 | i = max(i, 0); j = max(j, 0) |
| 26 | userlist = self.__class__() |
| 27 | userlist.data[:] = self.data[i:j] |
| 28 | return userlist |
| 29 | def __setslice__(self, i, j, other): |
| 30 | i = max(i, 0); j = max(j, 0) |
| 31 | if isinstance(other, UserList): |
| 32 | self.data[i:j] = other.data |
| 33 | elif isinstance(other, type(self.data)): |
| 34 | self.data[i:j] = other |
| 35 | else: |
| 36 | self.data[i:j] = list(other) |
| 37 | def __delslice__(self, i, j): |
| 38 | i = max(i, 0); j = max(j, 0) |
| 39 | del self.data[i:j] |
| 40 | def __add__(self, other): |
| 41 | if isinstance(other, UserList): |
| 42 | return self.__class__(self.data + other.data) |
| 43 | elif isinstance(other, type(self.data)): |
| 44 | return self.__class__(self.data + other) |
| 45 | else: |
| 46 | return self.__class__(self.data + list(other)) |
| 47 | def __radd__(self, other): |
| 48 | if isinstance(other, UserList): |
| 49 | return self.__class__(other.data + self.data) |
| 50 | elif isinstance(other, type(self.data)): |
| 51 | return self.__class__(other + self.data) |
| 52 | else: |
| 53 | return self.__class__(list(other) + self.data) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 54 | def __iadd__(self, other): |
| 55 | if isinstance(other, UserList): |
| 56 | self.data += other.data |
| 57 | elif isinstance(other, type(self.data)): |
| 58 | self.data += other |
| 59 | else: |
| 60 | self.data += list(other) |
| 61 | return self |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 62 | def __mul__(self, n): |
| 63 | return self.__class__(self.data*n) |
| 64 | __rmul__ = __mul__ |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 65 | def __imul__(self, n): |
| 66 | self.data *= n |
| 67 | return self |
Guido van Rossum | 2a340b3 | 1999-03-26 16:20:18 +0000 | [diff] [blame] | 68 | def append(self, item): self.data.append(item) |
| 69 | def insert(self, i, item): self.data.insert(i, item) |
| 70 | def pop(self, i=-1): return self.data.pop(i) |
| 71 | def remove(self, item): self.data.remove(item) |
| 72 | def count(self, item): return self.data.count(item) |
| 73 | def index(self, item): return self.data.index(item) |
| 74 | def reverse(self): self.data.reverse() |
| 75 | def sort(self, *args): apply(self.data.sort, args) |
| 76 | def extend(self, other): |
| 77 | if isinstance(other, UserList): |
| 78 | self.data.extend(other.data) |
| 79 | else: |
| 80 | self.data.extend(other) |