sequence.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. """
  15. Sequence Interfaces
  16. Importing this module does *not* mark any standard classes as
  17. implementing any of these interfaces.
  18. While this module is not deprecated, new code should generally use
  19. :mod:`zope.interface.common.collections`, specifically
  20. :class:`~zope.interface.common.collections.ISequence` and
  21. :class:`~zope.interface.common.collections.IMutableSequence`. This
  22. module is occasionally useful for its fine-grained breakdown of interfaces.
  23. The standard library :class:`list`, :class:`tuple` and
  24. :class:`collections.UserList`, among others, implement ``ISequence``
  25. or ``IMutableSequence`` but *do not* implement any of the interfaces
  26. in this module.
  27. """
  28. __docformat__ = 'restructuredtext'
  29. from zope.interface import Interface
  30. from zope.interface.common import collections
  31. from zope.interface._compat import PYTHON2 as PY2
  32. class IMinimalSequence(collections.IIterable):
  33. """Most basic sequence interface.
  34. All sequences are iterable. This requires at least one of the
  35. following:
  36. - a `__getitem__()` method that takes a single argument; integer
  37. values starting at 0 must be supported, and `IndexError` should
  38. be raised for the first index for which there is no value, or
  39. - an `__iter__()` method that returns an iterator as defined in
  40. the Python documentation (http://docs.python.org/lib/typeiter.html).
  41. """
  42. def __getitem__(index):
  43. """``x.__getitem__(index) <==> x[index]``
  44. Declaring this interface does not specify whether `__getitem__`
  45. supports slice objects."""
  46. class IFiniteSequence(collections.ISized, IMinimalSequence):
  47. """
  48. A sequence of bound size.
  49. .. versionchanged:: 5.0.0
  50. Extend ``ISized``
  51. """
  52. class IReadSequence(collections.IContainer, IFiniteSequence):
  53. """
  54. read interface shared by tuple and list
  55. This interface is similar to
  56. :class:`~zope.interface.common.collections.ISequence`, but
  57. requires that all instances be totally ordered. Most users
  58. should prefer ``ISequence``.
  59. .. versionchanged:: 5.0.0
  60. Extend ``IContainer``
  61. """
  62. def __contains__(item):
  63. """``x.__contains__(item) <==> item in x``"""
  64. # Optional in IContainer, required here.
  65. def __lt__(other):
  66. """``x.__lt__(other) <==> x < other``"""
  67. def __le__(other):
  68. """``x.__le__(other) <==> x <= other``"""
  69. def __eq__(other):
  70. """``x.__eq__(other) <==> x == other``"""
  71. def __ne__(other):
  72. """``x.__ne__(other) <==> x != other``"""
  73. def __gt__(other):
  74. """``x.__gt__(other) <==> x > other``"""
  75. def __ge__(other):
  76. """``x.__ge__(other) <==> x >= other``"""
  77. def __add__(other):
  78. """``x.__add__(other) <==> x + other``"""
  79. def __mul__(n):
  80. """``x.__mul__(n) <==> x * n``"""
  81. def __rmul__(n):
  82. """``x.__rmul__(n) <==> n * x``"""
  83. if PY2:
  84. def __getslice__(i, j):
  85. """``x.__getslice__(i, j) <==> x[i:j]``
  86. Use of negative indices is not supported.
  87. Deprecated since Python 2.0 but still a part of `UserList`.
  88. """
  89. class IExtendedReadSequence(IReadSequence):
  90. """Full read interface for lists"""
  91. def count(item):
  92. """Return number of occurrences of value"""
  93. def index(item, *args):
  94. """index(value, [start, [stop]]) -> int
  95. Return first index of *value*
  96. """
  97. class IUniqueMemberWriteSequence(Interface):
  98. """The write contract for a sequence that may enforce unique members"""
  99. def __setitem__(index, item):
  100. """``x.__setitem__(index, item) <==> x[index] = item``
  101. Declaring this interface does not specify whether `__setitem__`
  102. supports slice objects.
  103. """
  104. def __delitem__(index):
  105. """``x.__delitem__(index) <==> del x[index]``
  106. Declaring this interface does not specify whether `__delitem__`
  107. supports slice objects.
  108. """
  109. if PY2:
  110. def __setslice__(i, j, other):
  111. """``x.__setslice__(i, j, other) <==> x[i:j] = other``
  112. Use of negative indices is not supported.
  113. Deprecated since Python 2.0 but still a part of `UserList`.
  114. """
  115. def __delslice__(i, j):
  116. """``x.__delslice__(i, j) <==> del x[i:j]``
  117. Use of negative indices is not supported.
  118. Deprecated since Python 2.0 but still a part of `UserList`.
  119. """
  120. def __iadd__(y):
  121. """``x.__iadd__(y) <==> x += y``"""
  122. def append(item):
  123. """Append item to end"""
  124. def insert(index, item):
  125. """Insert item before index"""
  126. def pop(index=-1):
  127. """Remove and return item at index (default last)"""
  128. def remove(item):
  129. """Remove first occurrence of value"""
  130. def reverse():
  131. """Reverse *IN PLACE*"""
  132. def sort(cmpfunc=None):
  133. """Stable sort *IN PLACE*; `cmpfunc(x, y)` -> -1, 0, 1"""
  134. def extend(iterable):
  135. """Extend list by appending elements from the iterable"""
  136. class IWriteSequence(IUniqueMemberWriteSequence):
  137. """Full write contract for sequences"""
  138. def __imul__(n):
  139. """``x.__imul__(n) <==> x *= n``"""
  140. class ISequence(IReadSequence, IWriteSequence):
  141. """
  142. Full sequence contract.
  143. New code should prefer
  144. :class:`~zope.interface.common.collections.IMutableSequence`.
  145. Compared to that interface, which is implemented by :class:`list`
  146. (:class:`~zope.interface.common.builtins.IList`), among others,
  147. this interface is missing the following methods:
  148. - clear
  149. - count
  150. - index
  151. This interface adds the following methods:
  152. - sort
  153. """