0001 """A more or less complete user-defined wrapper around list objects.""" 0002 0003 class UserList: 0004 def __init__(self, initlist=None): 0005 self.data = [] 0006 if initlist is not None: 0007 # XXX should this accept an arbitrary sequence? 0008 if type(initlist) == type(self.data): 0009 self.data[:] = initlist 0010 elif isinstance(initlist, UserList): 0011 self.data[:] = initlist.data[:] 0012 else: 0013 self.data = list(initlist) 0014 def __repr__(self): return repr(self.data) 0015 def __lt__(self, other): return self.data < self.__cast(other) 0016 def __le__(self, other): return self.data <= self.__cast(other) 0017 def __eq__(self, other): return self.data == self.__cast(other) 0018 def __ne__(self, other): return self.data != self.__cast(other) 0019 def __gt__(self, other): return self.data > self.__cast(other) 0020 def __ge__(self, other): return self.data >= self.__cast(other) 0021 def __cast(self, other): 0022 if isinstance(other, UserList): return other.data 0023 else: return other 0024 def __cmp__(self, other): 0025 return cmp(self.data, self.__cast(other)) 0026 def __contains__(self, item): return item in self.data 0027 def __len__(self): return len(self.data) 0028 def __getitem__(self, i): return self.data[i] 0029 def __setitem__(self, i, item): self.data[i] = item 0030 def __delitem__(self, i): del self.data[i] 0031 def __getslice__(self, i, j): 0032 i = max(i, 0); j = max(j, 0) 0033 return self.__class__(self.data[i:j]) 0034 def __setslice__(self, i, j, other): 0035 i = max(i, 0); j = max(j, 0) 0036 if isinstance(other, UserList): 0037 self.data[i:j] = other.data 0038 elif isinstance(other, type(self.data)): 0039 self.data[i:j] = other 0040 else: 0041 self.data[i:j] = list(other) 0042 def __delslice__(self, i, j): 0043 i = max(i, 0); j = max(j, 0) 0044 del self.data[i:j] 0045 def __add__(self, other): 0046 if isinstance(other, UserList): 0047 return self.__class__(self.data + other.data) 0048 elif isinstance(other, type(self.data)): 0049 return self.__class__(self.data + other) 0050 else: 0051 return self.__class__(self.data + list(other)) 0052 def __radd__(self, other): 0053 if isinstance(other, UserList): 0054 return self.__class__(other.data + self.data) 0055 elif isinstance(other, type(self.data)): 0056 return self.__class__(other + self.data) 0057 else: 0058 return self.__class__(list(other) + self.data) 0059 def __iadd__(self, other): 0060 if isinstance(other, UserList): 0061 self.data += other.data 0062 elif isinstance(other, type(self.data)): 0063 self.data += other 0064 else: 0065 self.data += list(other) 0066 return self 0067 def __mul__(self, n): 0068 return self.__class__(self.data*n) 0069 __rmul__ = __mul__ 0070 def __imul__(self, n): 0071 self.data *= n 0072 return self 0073 def append(self, item): self.data.append(item) 0074 def insert(self, i, item): self.data.insert(i, item) 0075 def pop(self, i=-1): return self.data.pop(i) 0076 def remove(self, item): self.data.remove(item) 0077 def count(self, item): return self.data.count(item) 0078 def index(self, item, *args): return self.data.index(item, *args) 0079 def reverse(self): self.data.reverse() 0080 def sort(self, *args, **kwds): self.data.sort(*args, **kwds) 0081 def extend(self, other): 0082 if isinstance(other, UserList): 0083 self.data.extend(other.data) 0084 else: 0085 self.data.extend(other) 0086
Generated by PyXR 0.9.4