mapping.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. Mapping 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.IMapping` and
  21. :class:`~zope.interface.common.collections.IMutableMapping`. This
  22. module is occasionally useful for its extremely fine grained breakdown
  23. of interfaces.
  24. The standard library :class:`dict` and :class:`collections.UserDict`
  25. implement ``IMutableMapping``, but *do not* implement any of the
  26. interfaces in this module.
  27. """
  28. from zope.interface import Interface
  29. from zope.interface._compat import PYTHON2 as PY2
  30. from zope.interface.common import collections
  31. class IItemMapping(Interface):
  32. """Simplest readable mapping object
  33. """
  34. def __getitem__(key):
  35. """Get a value for a key
  36. A `KeyError` is raised if there is no value for the key.
  37. """
  38. class IReadMapping(collections.IContainer, IItemMapping):
  39. """
  40. Basic mapping interface.
  41. .. versionchanged:: 5.0.0
  42. Extend ``IContainer``
  43. """
  44. def get(key, default=None):
  45. """Get a value for a key
  46. The default is returned if there is no value for the key.
  47. """
  48. def __contains__(key):
  49. """Tell if a key exists in the mapping."""
  50. # Optional in IContainer, required by this interface.
  51. class IWriteMapping(Interface):
  52. """Mapping methods for changing data"""
  53. def __delitem__(key):
  54. """Delete a value from the mapping using the key."""
  55. def __setitem__(key, value):
  56. """Set a new item in the mapping."""
  57. class IEnumerableMapping(collections.ISized, IReadMapping):
  58. """
  59. Mapping objects whose items can be enumerated.
  60. .. versionchanged:: 5.0.0
  61. Extend ``ISized``
  62. """
  63. def keys():
  64. """Return the keys of the mapping object.
  65. """
  66. def __iter__():
  67. """Return an iterator for the keys of the mapping object.
  68. """
  69. def values():
  70. """Return the values of the mapping object.
  71. """
  72. def items():
  73. """Return the items of the mapping object.
  74. """
  75. class IMapping(IWriteMapping, IEnumerableMapping):
  76. ''' Simple mapping interface '''
  77. class IIterableMapping(IEnumerableMapping):
  78. """A mapping that has distinct methods for iterating
  79. without copying.
  80. On Python 2, a `dict` has these methods, but on Python 3
  81. the methods defined in `IEnumerableMapping` already iterate
  82. without copying.
  83. """
  84. if PY2:
  85. def iterkeys():
  86. "iterate over keys; equivalent to ``__iter__``"
  87. def itervalues():
  88. "iterate over values"
  89. def iteritems():
  90. "iterate over items"
  91. class IClonableMapping(Interface):
  92. """Something that can produce a copy of itself.
  93. This is available in `dict`.
  94. """
  95. def copy():
  96. "return copy of dict"
  97. class IExtendedReadMapping(IIterableMapping):
  98. """
  99. Something with a particular method equivalent to ``__contains__``.
  100. On Python 2, `dict` provides this method, but it was removed
  101. in Python 3.
  102. """
  103. if PY2:
  104. def has_key(key):
  105. """Tell if a key exists in the mapping; equivalent to ``__contains__``"""
  106. class IExtendedWriteMapping(IWriteMapping):
  107. """Additional mutation methods.
  108. These are all provided by `dict`.
  109. """
  110. def clear():
  111. "delete all items"
  112. def update(d):
  113. " Update D from E: for k in E.keys(): D[k] = E[k]"
  114. def setdefault(key, default=None):
  115. "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"
  116. def pop(k, default=None):
  117. """
  118. pop(k[,default]) -> value
  119. Remove specified key and return the corresponding value.
  120. If key is not found, *default* is returned if given, otherwise
  121. `KeyError` is raised. Note that *default* must not be passed by
  122. name.
  123. """
  124. def popitem():
  125. """remove and return some (key, value) pair as a
  126. 2-tuple; but raise KeyError if mapping is empty"""
  127. class IFullMapping(
  128. collections.IMutableMapping,
  129. IExtendedReadMapping, IExtendedWriteMapping, IClonableMapping, IMapping,):
  130. """
  131. Full mapping interface.
  132. Most uses of this interface should instead use
  133. :class:`~zope.interface.commons.collections.IMutableMapping` (one of the
  134. bases of this interface). The required methods are the same.
  135. .. versionchanged:: 5.0.0
  136. Extend ``IMutableMapping``
  137. """