(original) (raw)
On 13 September 2013 07:29, Tim Delaney <timothy.c.delaney@gmail.com> 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