Case-insensitive dict, non-destructive, fast, anyone?

Bengt Richter bokr at oz.net
Fri Apr 1 18:52:53 EST 2005


On Fri, 01 Apr 2005 23:04:42 GMT, "Raymond Hettinger" <vze4rx4y at verizon.net> wrote:

>[Bengt Richter]
>> I wonder if a dict with a general override hook for hashing all keys would be
>useful.
>> E.g.,  a dict.__keyhash__ that would take key as arg and default as now
>returning key.__hash__()
>> but that you could override. Seems like this could potentially be more
>efficient than key wrappers,
>> and would also make it so you wouldn't have to chase all the affected methods
>in doing an idict
>> like the above (e.g., get, setdefault, update etc. etc.)
>
>There has also been a discussion of adding a seqdict that maintains a keys in
>insertion order.  Another idea was to have a defaultdict that could be
>instructed to create values as necessary (either zero for counting or [] for
>list building).  Putting the three ideas together, perhaps we should write an
>extension module with a custom dictionary type with methods/attributes
>implementing those ideas.  Essentially, the thought is to put all the bells and
>whistles in one place.  If the extension became popular, it could ultimately
>wend its way into the collections module.
>
>A facetious naming idea would be to call it TrickyDict, a relative to DictMixin
>and no relation to a former U.S. president ;-)
>
>t = TrickyDict()
>t.keytransform(str.lower)
>t['abC'] = 1                  # case insensitive dictionary
>assert t['Abc'] == 1
 assert t.keys() == ['abC']    # actual keys unchanged (but transformed for hash and cmp)
[...]
>
>Taken together, these six attributes/methods could cover many wished for
>features for the 10% of the cases where a regular dictionary doesn't provide the
>best solution.
You think as much as 10% ?

Regards,
Bengt Richter



More information about the Python-list mailing list