On 14/09/2013 01:49, Steven D'Aprano wrote: > On Fri, Sep 13, 2013 at 11:45:16PM +0200, Antoine Pitrou wrote: > >> Ok, I have a better (IMO) proposal: >> >> >>> d = TransformDict(str.casefold, {'Foo': 1}) >> >>> d.getitem('foo') >> ('Foo', 1) >> >>> d.getitem('bar') >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> KeyError: 'bar' > > > Is it more common to want both the canonical key and value at the same > time, or to just want the canonical key? My gut feeling is that I'm > likely to have code like this: > > > d = TransformDict(...) > for key in data: > key = d.get_canonical(key) > value = d[key] > print("{}: {}".format(key, value)) > I think must be missing something. I thought that iterating over the dict would yield the original keys, so if you wanted the original key and value you would write: for key, value in data.items(): print("{}: {}".format(key, value)) and if you wanted the transformed key you would apply the transform function to the key. > > in which case having a single call to return both will be great: > > for key in data: > key, value = d.getitem(key) > print("{}: {}".format(key, value)) > > > but I'm really not sure. Maybe Ethan is right. > > I think these sorts of associated questions are why some people > (Raymond, Nick) want to see the proposal live outside of the standard > library for a while first. The general idea is great, but we're going to > bike shed the API without having much idea of how it will actually be > used. > > So, my suggestion is this: > > - Let's add __transform__ to dicts for 3.4, similar to __missing__, and > see how people end up using it in the real world. > > - Then, in 3.5, we can make a much better informed decision about the > best API for a TransformedDict front-end to __transform__. > > > +1 on __transform__ method on dicts. > > +0 on TransformedDict in 3.4 > > +1 on waiting for 3.5 based on experience on using __transform__. >
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4