mapping.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. """Mapping Interfaces
  15. """
  16. from zope.interface import Interface
  17. class IItemMapping(Interface):
  18. """Simplest readable mapping object
  19. """
  20. def __getitem__(key):
  21. """Get a value for a key
  22. A KeyError is raised if there is no value for the key.
  23. """
  24. class IReadMapping(IItemMapping):
  25. """Basic mapping interface
  26. """
  27. def get(key, default=None):
  28. """Get a value for a key
  29. The default is returned if there is no value for the key.
  30. """
  31. def __contains__(key):
  32. """Tell if a key exists in the mapping."""
  33. class IWriteMapping(Interface):
  34. """Mapping methods for changing data"""
  35. def __delitem__(key):
  36. """Delete a value from the mapping using the key."""
  37. def __setitem__(key, value):
  38. """Set a new item in the mapping."""
  39. class IEnumerableMapping(IReadMapping):
  40. """Mapping objects whose items can be enumerated.
  41. """
  42. def keys():
  43. """Return the keys of the mapping object.
  44. """
  45. def __iter__():
  46. """Return an iterator for the keys of the mapping object.
  47. """
  48. def values():
  49. """Return the values of the mapping object.
  50. """
  51. def items():
  52. """Return the items of the mapping object.
  53. """
  54. def __len__():
  55. """Return the number of items.
  56. """
  57. class IMapping(IWriteMapping, IEnumerableMapping):
  58. ''' Simple mapping interface '''
  59. class IIterableMapping(IEnumerableMapping):
  60. def iterkeys():
  61. "iterate over keys; equivalent to __iter__"
  62. def itervalues():
  63. "iterate over values"
  64. def iteritems():
  65. "iterate over items"
  66. class IClonableMapping(Interface):
  67. def copy():
  68. "return copy of dict"
  69. class IExtendedReadMapping(IIterableMapping):
  70. def has_key(key):
  71. """Tell if a key exists in the mapping; equivalent to __contains__"""
  72. class IExtendedWriteMapping(IWriteMapping):
  73. def clear():
  74. "delete all items"
  75. def update(d):
  76. " Update D from E: for k in E.keys(): D[k] = E[k]"
  77. def setdefault(key, default=None):
  78. "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"
  79. def pop(k, *args):
  80. """remove specified key and return the corresponding value
  81. *args may contain a single default value, or may not be supplied.
  82. If key is not found, default is returned if given, otherwise
  83. KeyError is raised"""
  84. def popitem():
  85. """remove and return some (key, value) pair as a
  86. 2-tuple; but raise KeyError if mapping is empty"""
  87. class IFullMapping(
  88. IExtendedReadMapping, IExtendedWriteMapping, IClonableMapping, IMapping):
  89. ''' Full mapping interface ''' # IMapping included so tests for IMapping
  90. # succeed with IFullMapping