This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ChainMap.new_child could use improvement
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: doerwalter, python-dev, r.david.murray, rhettinger, vinay.sajip
Priority: normal Keywords: patch

Created on 2012-12-05 09:13 by vinay.sajip, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
new-child.diff vinay.sajip, 2013-01-11 12:32 Code, test, and doc change for feature. review
Repositories containing patches
http://hg.python.org/sandbox/vsajip#fix16613
Messages (9)
msg176974 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2012-12-05 09:13
ChainMap.new_child could IMO be improved through allowing an optional dict to be passed, which is used to create the child. The use case is that you sometimes need to temporarily push a new non-empty mapping in front of an existing chain. This could be achieved by changing new_child to the following, which is backwards-compatible:

    def new_child(self, d=None):
        'New ChainMap with a new dict followed by all previous maps.'
        return self.__class__(d or {}, *self.maps)
msg179307 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-08 02:25
I agree that this would be useful.
msg179552 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2013-01-10 14:36
I'd like to have this feature too. However the code should use

   d if d is not None else {}

instead of

   d or {}

For example I might want to use a subclass of dict (lowerdict) that converts all keys to lowercase. When I use an empty lowerdict in new_child(), new_child() would silently use a normal dict instead:

   class lowerdict(dict):
       def __getitem__(self, key):
           return dict.__getitem__(
               self,
               key.lower() if isinstance(key, str) else key
           )
   
   import collections
   
   cm = collections.ChainMap(lowerdict(), lowerdict())
   
   cm2 = cm.new_child(lowerdict())
   
   print(type(cm2.maps[0]))

This would print <class 'dict'>.
msg179563 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2013-01-10 16:28
> d if d is not None else {}

Your intention makes sense, though I would prefer to write it as:

    if d is None:
        d = {}
    return self.__class__(d, *self.maps)
msg179671 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-01-11 11:30
Can you write-up a patch with tests and a doc change?
msg179674 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2013-01-11 12:36
> Can you write-up a patch with tests and a doc change?

Done.
msg179728 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-01-11 20:34
Put a *versionchanged* tag in the doc entry and this is ready to apply.
msg179736 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-01-11 22:14
Also, please change the variable name from *amap* to either *m* or *mapping*.
msg179745 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-11 23:40
New changeset c0ddae67f4df by Vinay Sajip in branch 'default':
Closes #16613: Added optional mapping argument to ChainMap.new_child.
http://hg.python.org/cpython/rev/c0ddae67f4df
History
Date User Action Args
2022-04-11 14:57:39adminsetgithub: 60817
2013-01-11 23:40:07python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg179745

resolution: fixed
stage: patch review -> resolved
2013-01-11 22:14:18rhettingersetmessages: + msg179736
2013-01-11 20:35:12rhettingersetassignee: rhettinger -> vinay.sajip
2013-01-11 20:34:25rhettingersetmessages: + msg179728
2013-01-11 12:36:20vinay.sajipsetmessages: + msg179674
stage: needs patch -> patch review
2013-01-11 12:32:22vinay.sajipsetfiles: + new-child.diff
keywords: + patch
2013-01-11 12:31:50vinay.sajipsethgrepos: + hgrepo171
2013-01-11 11:30:50rhettingersetmessages: + msg179671
stage: needs patch
2013-01-11 11:04:56rhettingersetassignee: rhettinger
2013-01-10 16:28:59vinay.sajipsetmessages: + msg179563
2013-01-10 14:36:20doerwaltersetnosy: + doerwalter
messages: + msg179552
2013-01-08 02:25:04r.david.murraysetnosy: + r.david.murray
messages: + msg179307
2012-12-05 09:13:11vinay.sajipcreate