home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / USERLIST.PY < prev    next >
Encoding:
Python Source  |  2000-10-06  |  3.1 KB  |  80 lines

  1. """A more or less complete user-defined wrapper around list objects."""
  2.  
  3. class UserList:
  4.     def __init__(self, initlist=None):
  5.         self.data = []
  6.         if initlist is not None:
  7.             # XXX should this accept an arbitrary sequence?
  8.             if type(initlist) == type(self.data):
  9.                 self.data[:] = initlist
  10.             elif isinstance(initlist, UserList):
  11.                 self.data[:] = initlist.data[:]
  12.             else:
  13.                 self.data = list(initlist)
  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 __contains__(self, item): return item in self.data
  21.     def __len__(self): return len(self.data)
  22.     def __getitem__(self, i): return self.data[i]
  23.     def __setitem__(self, i, item): self.data[i] = item
  24.     def __delitem__(self, i): del self.data[i]
  25.     def __getslice__(self, i, j):
  26.         i = max(i, 0); j = max(j, 0)
  27.         return self.__class__(self.data[i:j])
  28.     def __setslice__(self, i, j, other):
  29.         i = max(i, 0); j = max(j, 0)
  30.         if isinstance(other, UserList):
  31.             self.data[i:j] = other.data
  32.         elif isinstance(other, type(self.data)):
  33.             self.data[i:j] = other
  34.         else:
  35.             self.data[i:j] = list(other)
  36.     def __delslice__(self, i, j):
  37.         i = max(i, 0); j = max(j, 0)
  38.         del self.data[i:j]
  39.     def __add__(self, other):
  40.         if isinstance(other, UserList):
  41.             return self.__class__(self.data + other.data)
  42.         elif isinstance(other, type(self.data)):
  43.             return self.__class__(self.data + other)
  44.         else:
  45.             return self.__class__(self.data + list(other))
  46.     def __radd__(self, other):
  47.         if isinstance(other, UserList):
  48.             return self.__class__(other.data + self.data)
  49.         elif isinstance(other, type(self.data)):
  50.             return self.__class__(other + self.data)
  51.         else:
  52.             return self.__class__(list(other) + self.data)
  53.     def __iadd__(self, other):
  54.         if isinstance(other, UserList):
  55.             self.data += other.data
  56.         elif isinstance(other, type(self.data)):
  57.             self.data += other
  58.         else:
  59.             self.data += list(other)
  60.         return self
  61.     def __mul__(self, n):
  62.         return self.__class__(self.data*n)
  63.     __rmul__ = __mul__
  64.     def __imul__(self, n):
  65.         self.data *= n
  66.         return self
  67.     def append(self, item): self.data.append(item)
  68.     def insert(self, i, item): self.data.insert(i, item)
  69.     def pop(self, i=-1): return self.data.pop(i)
  70.     def remove(self, item): self.data.remove(item)
  71.     def count(self, item): return self.data.count(item)
  72.     def index(self, item): return self.data.index(item)
  73.     def reverse(self): self.data.reverse()
  74.     def sort(self, *args): apply(self.data.sort, args)
  75.     def extend(self, other):
  76.         if isinstance(other, UserList):
  77.             self.data.extend(other.data)
  78.         else:
  79.             self.data.extend(other)
  80.