mirror of
https://github.com/python/cpython.git
synced 2025-11-15 08:01:29 +00:00
Expand examples for ChainMap(). Improve markup.
This commit is contained in:
parent
9253862f45
commit
bfcb42936b
1 changed files with 72 additions and 36 deletions
|
|
@ -93,6 +93,37 @@ The class can be used to simulate nested scopes and is useful in templating.
|
|||
The use-cases also parallel those for the builtin :func:`super` function.
|
||||
A reference to ``d.parents`` is equivalent to: ``ChainMap(*d.maps[1:])``.
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
* The `MultiContext class
|
||||
<http://svn.enthought.com/svn/enthought/CodeTools/trunk/enthought/contexts/multi_context.py>`_
|
||||
in the Enthought `CodeTools package
|
||||
<https://github.com/enthought/codetools>`_ has options to support
|
||||
writing to any mapping in the chain.
|
||||
|
||||
* Django's `Context class
|
||||
<http://code.djangoproject.com/browser/django/trunk/django/template/context.py>`_
|
||||
for templating is a read-only chain of mappings. It also features
|
||||
pushing and popping of contexts similar to the
|
||||
:meth:`~collections.ChainMap.new_child` method and the
|
||||
:meth:`~collections.ChainMap.parents` property.
|
||||
|
||||
* The `Nested Contexts recipe
|
||||
<http://code.activestate.com/recipes/577434/>`_ has options to control
|
||||
whether writes and other mutations apply only to the first mapping or to
|
||||
any mapping in the chain.
|
||||
|
||||
* A `greatly simplified read-only version of Chainmap
|
||||
<http://code.activestate.com/recipes/305268/>`_.
|
||||
|
||||
|
||||
:class:`ChainMap` Examples and Recipes
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
This section shows various approaches to working with chained maps.
|
||||
|
||||
|
||||
Example of simulating Python's internal lookup chain::
|
||||
|
||||
import builtins
|
||||
|
|
@ -128,28 +159,33 @@ The class can be used to simulate nested scopes and is useful in templating.
|
|||
d.items() # All nested items
|
||||
dict(d) # Flatten into a regular dictionary
|
||||
|
||||
.. seealso::
|
||||
The :class:`ChainMap` class only makes updates (writes and deletions) to the
|
||||
first mapping in the chain while lookups will search the full chain. However,
|
||||
if deep writes and deletions are desired, it is easy to make a subclass that
|
||||
updates keys found deeper in the chain::
|
||||
|
||||
* The `MultiContext class
|
||||
<http://svn.enthought.com/svn/enthought/CodeTools/trunk/enthought/contexts/multi_context.py>`_
|
||||
in the Enthought `CodeTools package
|
||||
<https://github.com/enthought/codetools>`_ has options to support
|
||||
writing to any mapping in the chain.
|
||||
class DeepChainMap(ChainMap):
|
||||
'Variant of ChainMap that allows direct updates to inner scopes'
|
||||
|
||||
* Django's `Context class
|
||||
<http://code.djangoproject.com/browser/django/trunk/django/template/context.py>`_
|
||||
for templating is a read-only chain of mappings. It also features
|
||||
pushing and popping of contexts similar to the
|
||||
:meth:`~collections.ChainMap.new_child` method and the
|
||||
:meth:`~collections.ChainMap.parents` property.
|
||||
def __setitem__(self, key, value):
|
||||
for mapping in self.maps:
|
||||
if key in mapping:
|
||||
mapping[key] = value
|
||||
return
|
||||
self.maps[0][key] = value
|
||||
|
||||
* The `Nested Contexts recipe
|
||||
<http://code.activestate.com/recipes/577434/>`_ has options to control
|
||||
whether writes and other mutations apply only to the first mapping or to
|
||||
any mapping in the chain.
|
||||
def __delitem__(self, key):
|
||||
for mapping in self.maps:
|
||||
if key in mapping:
|
||||
del mapping[key]
|
||||
return
|
||||
raise KeyError(key)
|
||||
|
||||
* A `greatly simplified read-only version of Chainmap
|
||||
<http://code.activestate.com/recipes/305268/>`_.
|
||||
>>> d = DeepChainMap({'zebra': 'black'}, {'elephant' : 'blue'}, {'lion' : 'yellow'})
|
||||
>>> d['lion'] = 'orange' # update an existing key two levels down
|
||||
>>> d['snake'] = 'red' # new keys get added to the topmost dict
|
||||
>>> del d['elephant'] # remove an existing key one level down
|
||||
DeepChainMap({'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'})
|
||||
|
||||
|
||||
:class:`Counter` objects
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue