################################################################################ Copyright (c) 2001, 2002 Zope Foundation and Contributors.# All Rights Reserved.## This software is subject to the provisions of the Zope Public License,# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS# FOR A PARTICULAR PURPOSE.###############################################################################"""Mapping Interfaces.Importing this module does *not* mark any standard classes asimplementing any of these interfaces.While this module is not deprecated, new code should generally use:mod:`zope.interface.common.collections`, specifically:class:`~zope.interface.common.collections.IMapping` and:class:`~zope.interface.common.collections.IMutableMapping`. Thismodule is occasionally useful for its extremely fine grained breakdownof interfaces.The standard library :class:`dict` and :class:`collections.UserDict`implement ``IMutableMapping``, but *do not* implement any of theinterfaces in this module."""fromzope.interfaceimportInterfacefromzope.interface.commonimportcollections
[docs]classIItemMapping(Interface):"""Simplest readable mapping object """def__getitem__(key):"""Get a value for a key A `KeyError` is raised if there is no value for the key. """
[docs]classIReadMapping(collections.IContainer,IItemMapping):""" Basic mapping interface. .. versionchanged:: 5.0.0 Extend ``IContainer`` """defget(key,default=None):"""Get a value for a key The default is returned if there is no value for the key. """def__contains__(key):"""Tell if a key exists in the mapping."""
# Optional in IContainer, required by this interface.
[docs]classIWriteMapping(Interface):"""Mapping methods for changing data"""def__delitem__(key):"""Delete a value from the mapping using the key."""def__setitem__(key,value):"""Set a new item in the mapping."""
[docs]classIEnumerableMapping(collections.ISized,IReadMapping):""" Mapping objects whose items can be enumerated. .. versionchanged:: 5.0.0 Extend ``ISized`` """defkeys():"""Return the keys of the mapping object. """def__iter__():"""Return an iterator for the keys of the mapping object. """defvalues():"""Return the values of the mapping object. """defitems():"""Return the items of the mapping object. """
[docs]classIIterableMapping(IEnumerableMapping):"""A mapping that has distinct methods for iterating without copying. """
[docs]classIClonableMapping(Interface):"""Something that can produce a copy of itself. This is available in `dict`. """defcopy():"return copy of dict"
[docs]classIExtendedReadMapping(IIterableMapping):""" Something with a particular method equivalent to ``__contains__``. On Python 2, `dict` provided the ``has_key`` method, but it was removed in Python 3. """
[docs]classIExtendedWriteMapping(IWriteMapping):"""Additional mutation methods. These are all provided by `dict`. """defclear():"delete all items"defupdate(d):" Update D from E: for k in E.keys(): D[k] = E[k]"defsetdefault(key,default=None):"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"defpop(k,default=None):""" pop(k[,default]) -> value Remove specified key and return the corresponding value. If key is not found, *default* is returned if given, otherwise `KeyError` is raised. Note that *default* must not be passed by name. """defpopitem():"""remove and return some (key, value) pair as a 2-tuple; but raise KeyError if mapping is empty"""
[docs]classIFullMapping(collections.IMutableMapping,IExtendedReadMapping,IExtendedWriteMapping,IClonableMapping,IMapping,):""" Full mapping interface. Most uses of this interface should instead use :class:`~zope.interface.commons.collections.IMutableMapping` (one of the bases of this interface). The required methods are the same. .. versionchanged:: 5.0.0 Extend ``IMutableMapping`` """