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

Nick Coghlan ncoghlan at gmail.com
Wed Sep 11 14:47:12 CEST 2013


On 11 September 2013 21:57, R. David Murray <rdmurray at bitdance.com> wrote:
> Except it is wider than that: the transform function can be anything,
> not just case folding.
>
> I suggested surjectiondict or ontodict, but Antoine didn't like those :)
> (I had to look up the terms...it's been a long time since I studied
> math.)

I'll join the chorus requesting that this live on PyPI for a while first.

I think this is a case similar to what happened with
contextlib.ExitStack: I'm not sure if anyone actually *used*
contextlib2 for anything significant (if they did, they didn't tell me
about it), but just going through the process of properly documenting,
publishing and testing it as a relatively independent project forced
*me* to think through the design. Add in a couple of interesting
conversation with Brandon Rhodes and others about the API design, and
the end result was a *vast* improvement over what originally went up
on PyPI.

The barriers to making use of PyPI libraries are also falling with
time, so a solid implementation may still see adoption, even if it's
in the form of copy-and-paste programming rather than actual
dependencies.

I think there are also additional API decisions to be made beyond just
the one about how to declare the mapping function, related to how to
get the mapped key values *out*, as well as how to find out whether or
not two potential key values map to the same actual key.

As my preferred bikeshed colour, I'm going to suggest "MappedKeyDict"
(using a similar naming style to OrderedDict), since the purpose of
the container is to map the domain of the supplied keys to a different
range before doing the value lookup.

Suggested additional methods:

    md.map_key(key)  # Applies the mapping function to the supplied key
    md.mapped_keys()  # Like keys(), but with the key mapping function applied
    md.mapped_items()  # Like items(), but with the key mapping function applied

Another (more dubious) possible method:

    md.same_key(key1, key2)  # "md.map_key(key1) == md.map_key(key2)"

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list