Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 1 | # A more or less complete user-defined wrapper around list objects |
| 2 | |
| 3 | class UserList: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4 | def __init__(self, list = None): |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 5 | self.data = [] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6 | if list is not None: |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 7 | if type(list) == type(self.data): |
| 8 | self.data[:] = list |
| 9 | else: |
| 10 | self.data[:] = list.data[:] |
| 11 | def __repr__(self): return repr(self.data) |
| 12 | def __cmp__(self, list): |
| 13 | if type(list) == type(self.data): |
| 14 | return cmp(self.data, list) |
| 15 | else: |
| 16 | return cmp(self.data, list.data) |
| 17 | def __len__(self): return len(self.data) |
| 18 | def __getitem__(self, i): return self.data[i] |
| 19 | def __setitem__(self, i, item): self.data[i] = item |
| 20 | def __delitem__(self, i): del self.data[i] |
| 21 | def __getslice__(self, i, j): |
| 22 | userlist = UserList() |
| 23 | userlist.data[:] = self.data[i:j] |
| 24 | return userlist |
| 25 | def __setslice__(self, i, j, list): |
| 26 | if type(list) == type(self.data): |
| 27 | self.data[i:j] = list |
| 28 | else: |
| 29 | self.data[i:j] = list.data |
| 30 | def __delslice__(self, i, j): del self.data[i:j] |
Guido van Rossum | ce84920 | 1996-05-28 22:56:16 +0000 | [diff] [blame] | 31 | def __add__(self, list): |
| 32 | if type(list) == type(self.data): |
| 33 | return self.__class__(self.data + list) |
| 34 | else: |
| 35 | return self.__class__(self.data + list.data) |
| 36 | def __radd__(self, list): |
| 37 | if type(list) == type(self.data): |
| 38 | return self.__class__(list + self.data) |
| 39 | else: |
| 40 | return self.__class__(list.data + self.data) |
| 41 | def __mul__(self, n): |
| 42 | return self.__class__(self.data*n) |
| 43 | __rmul__ = __mul__ |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 44 | def append(self, item): self.data.append(item) |
| 45 | def insert(self, i, item): self.data.insert(i, item) |
Guido van Rossum | bf0db03 | 1998-06-30 15:40:05 +0000 | [diff] [blame] | 46 | def pop(self, i=-1): return self.data.pop(i) |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 47 | def remove(self, item): self.data.remove(item) |
| 48 | def count(self, item): return self.data.count(item) |
| 49 | def index(self, item): return self.data.index(item) |
| 50 | def reverse(self): self.data.reverse() |
| 51 | def sort(self, *args): apply(self.data.sort, args) |