sequence.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001, 2002 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Sequence Interfaces
  15. """
  16. __docformat__ = 'restructuredtext'
  17. from zope.interface import Interface
  18. class IMinimalSequence(Interface):
  19. """Most basic sequence interface.
  20. All sequences are iterable. This requires at least one of the
  21. following:
  22. - a `__getitem__()` method that takes a single argument; interger
  23. values starting at 0 must be supported, and `IndexError` should
  24. be raised for the first index for which there is no value, or
  25. - an `__iter__()` method that returns an iterator as defined in
  26. the Python documentation (http://docs.python.org/lib/typeiter.html).
  27. """
  28. def __getitem__(index):
  29. """`x.__getitem__(index)` <==> `x[index]`
  30. Declaring this interface does not specify whether `__getitem__`
  31. supports slice objects."""
  32. class IFiniteSequence(IMinimalSequence):
  33. def __len__():
  34. """`x.__len__()` <==> `len(x)`"""
  35. class IReadSequence(IFiniteSequence):
  36. """read interface shared by tuple and list"""
  37. def __contains__(item):
  38. """`x.__contains__(item)` <==> `item in x`"""
  39. def __lt__(other):
  40. """`x.__lt__(other)` <==> `x < other`"""
  41. def __le__(other):
  42. """`x.__le__(other)` <==> `x <= other`"""
  43. def __eq__(other):
  44. """`x.__eq__(other)` <==> `x == other`"""
  45. def __ne__(other):
  46. """`x.__ne__(other)` <==> `x != other`"""
  47. def __gt__(other):
  48. """`x.__gt__(other)` <==> `x > other`"""
  49. def __ge__(other):
  50. """`x.__ge__(other)` <==> `x >= other`"""
  51. def __add__(other):
  52. """`x.__add__(other)` <==> `x + other`"""
  53. def __mul__(n):
  54. """`x.__mul__(n)` <==> `x * n`"""
  55. def __rmul__(n):
  56. """`x.__rmul__(n)` <==> `n * x`"""
  57. def __getslice__(i, j):
  58. """`x.__getslice__(i, j)` <==> `x[i:j]`
  59. Use of negative indices is not supported.
  60. Deprecated since Python 2.0 but still a part of `UserList`.
  61. """
  62. class IExtendedReadSequence(IReadSequence):
  63. """Full read interface for lists"""
  64. def count(item):
  65. """Return number of occurrences of value"""
  66. def index(item, *args):
  67. """Return first index of value
  68. `L.index(value, [start, [stop]])` -> integer"""
  69. class IUniqueMemberWriteSequence(Interface):
  70. """The write contract for a sequence that may enforce unique members"""
  71. def __setitem__(index, item):
  72. """`x.__setitem__(index, item)` <==> `x[index] = item`
  73. Declaring this interface does not specify whether `__setitem__`
  74. supports slice objects.
  75. """
  76. def __delitem__(index):
  77. """`x.__delitem__(index)` <==> `del x[index]`
  78. Declaring this interface does not specify whether `__delitem__`
  79. supports slice objects.
  80. """
  81. def __setslice__(i, j, other):
  82. """`x.__setslice__(i, j, other)` <==> `x[i:j]=other`
  83. Use of negative indices is not supported.
  84. Deprecated since Python 2.0 but still a part of `UserList`.
  85. """
  86. def __delslice__(i, j):
  87. """`x.__delslice__(i, j)` <==> `del x[i:j]`
  88. Use of negative indices is not supported.
  89. Deprecated since Python 2.0 but still a part of `UserList`.
  90. """
  91. def __iadd__(y):
  92. """`x.__iadd__(y)` <==> `x += y`"""
  93. def append(item):
  94. """Append item to end"""
  95. def insert(index, item):
  96. """Insert item before index"""
  97. def pop(index=-1):
  98. """Remove and return item at index (default last)"""
  99. def remove(item):
  100. """Remove first occurrence of value"""
  101. def reverse():
  102. """Reverse *IN PLACE*"""
  103. def sort(cmpfunc=None):
  104. """Stable sort *IN PLACE*; `cmpfunc(x, y)` -> -1, 0, 1"""
  105. def extend(iterable):
  106. """Extend list by appending elements from the iterable"""
  107. class IWriteSequence(IUniqueMemberWriteSequence):
  108. """Full write contract for sequences"""
  109. def __imul__(n):
  110. """`x.__imul__(n)` <==> `x *= n`"""
  111. class ISequence(IReadSequence, IWriteSequence):
  112. """Full sequence contract"""