from__future__importunicode_literalsfrom.importstrings# from jaraco.util.dictlibclassKeyTransformingDict(dict):""" A dict subclass that transforms the keys before they're used. Subclasses may override the default key_transform to customize behavior. """@staticmethoddefkey_transform(key):returnkeydef__init__(self,*args,**kargs):super(KeyTransformingDict,self).__init__()# build a dictionary using the default constructsd=dict(*args,**kargs)# build this dictionary using transformed keys.foritemind.items():self.__setitem__(*item)def__setitem__(self,key,val):key=self.key_transform(key)super(KeyTransformingDict,self).__setitem__(key,val)def__getitem__(self,key):key=self.key_transform(key)returnsuper(KeyTransformingDict,self).__getitem__(key)def__contains__(self,key):key=self.key_transform(key)returnsuper(KeyTransformingDict,self).__contains__(key)def__delitem__(self,key):key=self.key_transform(key)returnsuper(KeyTransformingDict,self).__delitem__(key)defsetdefault(self,key,*args,**kwargs):key=self.key_transform(key)returnsuper(KeyTransformingDict,self).setdefault(key,*args,**kwargs)defpop(self,key,*args,**kwargs):key=self.key_transform(key)returnsuper(KeyTransformingDict,self).pop(key,*args,**kwargs)classIRCDict(KeyTransformingDict):""" A dictionary of names whose keys are case-insensitive according to the IRC RFC rules. >>> d = IRCDict({'[This]': 'that'}, A='foo') The dict maintains the original case: >>> d.keys() [u'A', u'[This]'] But the keys can be referenced with a different case >>> d['a'] u'foo' >>> d['{this}'] u'that' >>> d['{THIS}'] u'that' >>> '{thiS]' in d True This should work for operations like delete and pop as well. >>> d.pop('A') u'foo' >>> del d['{This}'] >>> len(d) 0 """@staticmethoddefkey_transform(key):ifisinstance(key,basestring):key=strings.IRCFoldedCase(key)returnkey