[Python-Dev] Add a "transformdict" to collections

Ethan Furman ethan at stoneleaf.us
Wed Sep 11 18:03:18 CEST 2013


On 09/11/2013 08:49 AM, Victor Stinner wrote:
> 2013/9/11 Ethan Furman <ethan at stoneleaf.us>:
>> He isn't keeping the key unchanged (notice no white space in MAPPING), he's
>> merely providing a function that will automatically strip the whitespace
>> from key lookups.
>
> transformdict keeps the key unchanged, see the first message:
>
>     >>> d = transformdict(str.lower)
>     >>> d['Foo'] = 5
>     >>> d['foo']
>     5
>     >>> d['FOO']
>     5
>     >>> list(d)
>     ['Foo']
>
> 'Foo' is stored as 'Foo', not as 'foo'. So for stripped keys:
>
> d=transformdict(str.strip); d['   abc   ']; print(list(d))
>
> should print "['   abc   ']", not "['abc']".

And indeed it does:

Python 3.4.0a1+ (default:833246d42825+, Aug 31 2013, 14:17:59)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
--> from collections import transformdict
--> d=transformdict(str.strip); d['   abc   '] = 42; print(list(d))
['   abc   ']

> Is it the expected result?

Yup!  :)

--
~Ethan~


More information about the Python-Dev mailing list