Issue 16613: ChainMap.new_child could use improvement (original) (raw)

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:39 admin set github: 60817
2013-01-11 23:40:07 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: patch review -> resolved
2013-01-11 22:14:18 rhettinger set messages: +
2013-01-11 20:35:12 rhettinger set assignee: rhettinger -> vinay.sajip
2013-01-11 20:34:25 rhettinger set messages: +
2013-01-11 12:36:20 vinay.sajip set messages: + stage: needs patch -> patch review
2013-01-11 12:32:22 vinay.sajip set files: + new-child.diffkeywords: + patch
2013-01-11 12:31:50 vinay.sajip set hgrepos: + hgrepo171
2013-01-11 11:30:50 rhettinger set messages: + stage: needs patch
2013-01-11 11:04:56 rhettinger set assignee: rhettinger
2013-01-10 16:28:59 vinay.sajip set messages: +
2013-01-10 14:36:20 doerwalter set nosy: + doerwaltermessages: +
2013-01-08 02:25:04 r.david.murray set nosy: + r.david.murraymessages: +
2012-12-05 09:13:11 vinay.sajip create