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/128568.html below:

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

[Python-Dev] Add a "transformdict" to collectionsSteven D'Aprano steve at pearwood.info
Wed Sep 11 15:40:32 CEST 2013
On Wed, Sep 11, 2013 at 06:08:25AM -0500, Skip Montanaro wrote:

> (I still don't care for the name. "Transform" != "case folding" in my
> mind. A quick scan of your links suggests most people think something
> like "cidict" or "CaseInsensitiveDict" would be more descriptive.)

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.

Using Antoine's proposal:

MAPPING = TransformDict(str.strip)
MAPPING.update({'spam': 23, 'ham': 42, 'eggs': 17})
result = MAPPING[key]
# later...
answer = MAPPING[key]  # Fine now.

so that the mapping handles stripping the keys, not the caller.

This isn't just about strings, and certainly not just 
case-insensitivity.



-- 
Steven
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