You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sequence.py 4.7KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. Importing this module does *not* mark any standard classes
  16. as implementing any of these interfaces.
  17. """
  18. __docformat__ = 'restructuredtext'
  19. from zope.interface import Interface
  20. class IMinimalSequence(Interface):
  21. """Most basic sequence interface.
  22. All sequences are iterable. This requires at least one of the
  23. following:
  24. - a `__getitem__()` method that takes a single argument; integer
  25. values starting at 0 must be supported, and `IndexError` should
  26. be raised for the first index for which there is no value, or
  27. - an `__iter__()` method that returns an iterator as defined in
  28. the Python documentation (http://docs.python.org/lib/typeiter.html).
  29. """
  30. def __getitem__(index):
  31. """``x.__getitem__(index) <==> x[index]``
  32. Declaring this interface does not specify whether `__getitem__`
  33. supports slice objects."""
  34. class IFiniteSequence(IMinimalSequence):
  35. def __len__():
  36. """``x.__len__() <==> len(x)``"""
  37. class IReadSequence(IFiniteSequence):
  38. """read interface shared by tuple and list"""
  39. def __contains__(item):
  40. """``x.__contains__(item) <==> item in x``"""
  41. def __lt__(other):
  42. """``x.__lt__(other) <==> x < other``"""
  43. def __le__(other):
  44. """``x.__le__(other) <==> x <= other``"""
  45. def __eq__(other):
  46. """``x.__eq__(other) <==> x == other``"""
  47. def __ne__(other):
  48. """``x.__ne__(other) <==> x != other``"""
  49. def __gt__(other):
  50. """``x.__gt__(other) <==> x > other``"""
  51. def __ge__(other):
  52. """``x.__ge__(other) <==> x >= other``"""
  53. def __add__(other):
  54. """``x.__add__(other) <==> x + other``"""
  55. def __mul__(n):
  56. """``x.__mul__(n) <==> x * n``"""
  57. def __rmul__(n):
  58. """``x.__rmul__(n) <==> n * x``"""
  59. def __getslice__(i, j):
  60. """``x.__getslice__(i, j) <==> x[i:j]``
  61. Use of negative indices is not supported.
  62. Deprecated since Python 2.0 but still a part of `UserList`.
  63. """
  64. class IExtendedReadSequence(IReadSequence):
  65. """Full read interface for lists"""
  66. def count(item):
  67. """Return number of occurrences of value"""
  68. def index(item, *args):
  69. """index(value, [start, [stop]]) -> int
  70. Return first index of *value*
  71. """
  72. class IUniqueMemberWriteSequence(Interface):
  73. """The write contract for a sequence that may enforce unique members"""
  74. def __setitem__(index, item):
  75. """``x.__setitem__(index, item) <==> x[index] = item``
  76. Declaring this interface does not specify whether `__setitem__`
  77. supports slice objects.
  78. """
  79. def __delitem__(index):
  80. """``x.__delitem__(index) <==> del x[index]``
  81. Declaring this interface does not specify whether `__delitem__`
  82. supports slice objects.
  83. """
  84. def __setslice__(i, j, other):
  85. """``x.__setslice__(i, j, other) <==> x[i:j] = other``
  86. Use of negative indices is not supported.
  87. Deprecated since Python 2.0 but still a part of `UserList`.
  88. """
  89. def __delslice__(i, j):
  90. """``x.__delslice__(i, j) <==> del x[i:j]``
  91. Use of negative indices is not supported.
  92. Deprecated since Python 2.0 but still a part of `UserList`.
  93. """
  94. def __iadd__(y):
  95. """``x.__iadd__(y) <==> x += y``"""
  96. def append(item):
  97. """Append item to end"""
  98. def insert(index, item):
  99. """Insert item before index"""
  100. def pop(index=-1):
  101. """Remove and return item at index (default last)"""
  102. def remove(item):
  103. """Remove first occurrence of value"""
  104. def reverse():
  105. """Reverse *IN PLACE*"""
  106. def sort(cmpfunc=None):
  107. """Stable sort *IN PLACE*; `cmpfunc(x, y)` -> -1, 0, 1"""
  108. def extend(iterable):
  109. """Extend list by appending elements from the iterable"""
  110. class IWriteSequence(IUniqueMemberWriteSequence):
  111. """Full write contract for sequences"""
  112. def __imul__(n):
  113. """``x.__imul__(n) <==> x *= n``"""
  114. class ISequence(IReadSequence, IWriteSequence):
  115. """Full sequence contract"""