Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 1 | # A more or less complete user-defined wrapper around list objects |
| 2 | |
| 3 | class UserList: |
| 4 | def __init__(self, list = None): |
| 5 | self.data = [] |
| 6 | if list is not None: |
| 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] |
| 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__ |
| 44 | def append(self, item): self.data.append(item) |
| 45 | def insert(self, i, item): self.data.insert(i, item) |
| 46 | def remove(self, item): self.data.remove(item) |
| 47 | def count(self, item): return self.data.count(item) |
| 48 | def index(self, item): return self.data.index(item) |
| 49 | def reverse(self): self.data.reverse() |
| 50 | def sort(self, *args): apply(self.data.sort, args) |