On 13 September 2013 07:29, Tim Delaney <[email protected]> wrote:
>
> In this case though, there are two pieces of information:
>
> 1. A canonical key (which may or may not equal the original key);
>
> 2. The original key.
>
> It seems to me then that TransformDict is a specialised case of
> CanonicalDict, where the canonical key is defined to be the first key
> inserted. It would in fact be possible (though inefficient) to implement
> that using a canonicalising callable that maintained state - something like
> (untested):
>
> class OriginalKeys:
> def __init__(self)::
> self.keys = CanonicalDict(str.lower)
>
> def __call__(self, key):
> return self.keys.setdefault(key, key)
>
> class OriginalKeyDict(CanonicalDict):
> def __init__(self)::
> super().__init__(OriginalKeys())
>
Bah - got myself mixed up with original key and case preserving there ...
try this:
class OriginalKeys:
def __init__(self, func)::
self.keys = CanonicalDict(func)
def __call__(self, key):
return self.keys.setdefault(key, key)
class OriginalKeyDict(CanonicalDict):
def __init__(self, func)::
super().__init__(OriginalKeys(func))
class IdentityDict(OriginalKeyDict):
def __init__(self):
super().__init__(id)
class CasePreservingDict(OriginalKeyDict):
def __init__(self):
super().__init__(str.lower)
Tim Delaney
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com