UserList.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """A more or less complete user-defined wrapper around list objects."""
  2. import collections
  3. class UserList(collections.MutableSequence):
  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 __lt__(self, other): return self.data < self.__cast(other)
  16. def __le__(self, other): return self.data <= self.__cast(other)
  17. def __eq__(self, other): return self.data == self.__cast(other)
  18. def __ne__(self, other): return self.data != self.__cast(other)
  19. def __gt__(self, other): return self.data > self.__cast(other)
  20. def __ge__(self, other): return self.data >= self.__cast(other)
  21. def __cast(self, other):
  22. if isinstance(other, UserList): return other.data
  23. else: return other
  24. def __cmp__(self, other):
  25. return cmp(self.data, self.__cast(other))
  26. __hash__ = None # Mutable sequence, so not hashable
  27. def __contains__(self, item): return item in self.data
  28. def __len__(self): return len(self.data)
  29. def __getitem__(self, i): return self.data[i]
  30. def __setitem__(self, i, item): self.data[i] = item
  31. def __delitem__(self, i): del self.data[i]
  32. def __getslice__(self, i, j):
  33. i = max(i, 0); j = max(j, 0)
  34. return self.__class__(self.data[i:j])
  35. def __setslice__(self, i, j, other):
  36. i = max(i, 0); j = max(j, 0)
  37. if isinstance(other, UserList):
  38. self.data[i:j] = other.data
  39. elif isinstance(other, type(self.data)):
  40. self.data[i:j] = other
  41. else:
  42. self.data[i:j] = list(other)
  43. def __delslice__(self, i, j):
  44. i = max(i, 0); j = max(j, 0)
  45. del self.data[i:j]
  46. def __add__(self, other):
  47. if isinstance(other, UserList):
  48. return self.__class__(self.data + other.data)
  49. elif isinstance(other, type(self.data)):
  50. return self.__class__(self.data + other)
  51. else:
  52. return self.__class__(self.data + list(other))
  53. def __radd__(self, other):
  54. if isinstance(other, UserList):
  55. return self.__class__(other.data + self.data)
  56. elif isinstance(other, type(self.data)):
  57. return self.__class__(other + self.data)
  58. else:
  59. return self.__class__(list(other) + self.data)
  60. def __iadd__(self, other):
  61. if isinstance(other, UserList):
  62. self.data += other.data
  63. elif isinstance(other, type(self.data)):
  64. self.data += other
  65. else:
  66. self.data += list(other)
  67. return self
  68. def __mul__(self, n):
  69. return self.__class__(self.data*n)
  70. __rmul__ = __mul__
  71. def __imul__(self, n):
  72. self.data *= n
  73. return self
  74. def append(self, item): self.data.append(item)
  75. def insert(self, i, item): self.data.insert(i, item)
  76. def pop(self, i=-1): return self.data.pop(i)
  77. def remove(self, item): self.data.remove(item)
  78. def count(self, item): return self.data.count(item)
  79. def index(self, item, *args): return self.data.index(item, *args)
  80. def reverse(self): self.data.reverse()
  81. def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
  82. def extend(self, other):
  83. if isinstance(other, UserList):
  84. self.data.extend(other.data)
  85. else:
  86. self.data.extend(other)