A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2013-September/128579.html below:

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

[Python-Dev] Add a "transformdict" to collectionsEthan Furman ethan at stoneleaf.us
Wed Sep 11 16:27:44 CEST 2013
On 09/11/2013 06:58 AM, Victor Stinner wrote:
> 2013/9/11 Steven D'Aprano <steve at pearwood.info>:
>> But the proposal is not for a case-insensitive dict. It is more general
>> than that, with case-insensitivity just one specific use-case for such
>> a transformative dict. Arguably the most natural, or at least obvious,
>> such transformation, but there are others.
>>
>> I have code that does something like this:
>>
>> MAPPING = {'spam': 23, 'ham': 42, 'eggs': 17}
>> result = MAPPING[key.strip()]
>> # later...
>> answer = MAPPING[key]  # Oops, forgot to strip! This is broken.
>
> For this use case, you should not keep the key unchanged, but it's
> better to store the stripped key (so MAPPING.keys() gives you the
> expected result).

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.

> The transformdict type proposed by Antoine cannot be
> used for this use case.

Yes, it can.

--> from collections import transformdict
--> MAPPING = transformdict(str.strip)
--> MAPPING.update({'spam': 23, 'ham': 42, 'eggs': 17})
--> MAPPING
transformdict(<method 'strip' of 'str' objects>, {'ham': 42, 'spam': 23, 'eggs': 17})
--> MAPPING[' eggs ']
17

--
~Ethan~
More information about the Python-Dev mailing list

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