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

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

[Python-Dev] Add a "transformdict" to collections [Python-Dev] Add a "transformdict" to collectionsAntoine Pitrou solipsis at pitrou.net
Tue Sep 10 11:28:32 CEST 2013
Hello,

In http://bugs.python.org/issue18986 I proposed adding a new mapping
type to the collections module.

The original use case is quite common in network programming and
elsewhere (Eric Snow on the tracker mentioned an application with stock
symbols). You want to have an associative container which matches keys
case-insensitively but also preserves the original casing (e.g. for
presentation). It is a commonly reimplemented container.

It is also an instance of a more general pattern: match keys after
applying a derivation (or coercion) function, but at the same time keep
track of the original key. Note that the derivation function needn't be
(and generally won't be) bijective, otherwise it's too simple.

Therefore I propose adding the general pattern. Simple example:

   >>> d = transformdict(str.lower)
   >>> d['Foo'] = 5
   >>> d['foo']
   5
   >>> d['FOO']
   5
   >>> list(d)
   ['Foo']

(case-insensitive but case-preserving, as the best filesystems are ;-))


On the tracker issue, it seems everyone agreed on the principle. There
is some bikeshedding left to do, though. So here are the reasonable
naming proposals so far:

- transformkeydict
- coercekeydict
- transformdict
- coercedict

I have a sweet spot for "transformdict" myself.

Regards

Antoine.


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