Commits

Jason R. Coombs committed dc83a82

Replaced FoldedCaseKeyedDict with KeyTransformingDict (a more general-purpose base class designed specifically to transform keys). This implementation has more complete implementation of all key-relevant operations (pop, del, setdefault).
Expanded documentation on IRCDict to include more complete tests.

  • Participants
  • Parent commits 2b58692

Comments (0)

Files changed (1)

         else:
             return None
 
-# based on jaraco.util.dictlib
-class FoldedCaseKeyedDict(dict):
-    """A case-insensitive dictionary (keys are compared as insensitive
-    if they are strings).
-    >>> d = FoldedCaseKeyedDict()
-    >>> d['heLlo'] = 'world'
-    >>> d
-    {'heLlo': 'world'}
-    >>> d['hello']
-    'world'
-    >>> 'hello' in d
-    True
-    >>> 'HELLO' in d
-    True
-    >>> FoldedCaseKeyedDict({'heLlo': 'world'})
-    {'heLlo': 'world'}
-    >>> d = FoldedCaseKeyedDict({'heLlo': 'world'})
-    >>> d['hello']
-    'world'
-    >>> d['Hello']
-    'world'
-    >>> d
-    {'heLlo': 'world'}
-    >>> d = FoldedCaseKeyedDict({'heLlo': 'world', 'Hello': 'world'})
-    >>> d
-    {'heLlo': 'world'}
+# from jaraco.util.dictlib
+class KeyTransformingDict(dict):
     """
-
+    A dict subclass that transforms the keys before they're used.
+    Subclasses may override the default key_transform to customize behavior.
+    """
     @staticmethod
     def key_transform(key):
-        if isinstance(key, basestring):
-            key = irclib.FoldedCase(key)
         return key
 
     def __init__(self, *args, **kargs):
-        super(FoldedCaseKeyedDict, self).__init__()
+        super(KeyTransformingDict, self).__init__()
         # build a dictionary using the default constructs
         d = dict(*args, **kargs)
-        # build this dictionary using case insensitivity.
+        # build this dictionary using transformed keys.
         for item in d.items():
             self.__setitem__(*item)
 
     def __setitem__(self, key, val):
         key = self.key_transform(key)
-        super(FoldedCaseKeyedDict, self).__setitem__(key, val)
+        super(KeyTransformingDict, self).__setitem__(key, val)
 
     def __getitem__(self, key):
         key = self.key_transform(key)
-        return super(FoldedCaseKeyedDict, self).__getitem__(key)
+        return super(KeyTransformingDict, self).__getitem__(key)
 
     def __contains__(self, key):
         key = self.key_transform(key)
-        return super(FoldedCaseKeyedDict, self).__contains__(key)
+        return super(KeyTransformingDict, self).__contains__(key)
 
-class IRCDict(FoldedCaseKeyedDict):
+    def __delitem__(self, key):
+        key = self.key_transform(key)
+        return super(KeyTransformingDict, self).__delitem__(key)
+
+    def setdefault(self, key, *args, **kwargs):
+        key = self.key_transform(key)
+        return super(KeyTransformingDict, self).setdefault(key, *args, **kwargs)
+
+    def pop(self, key, *args, **kwargs):
+        key = self.key_transform(key)
+        return super(KeyTransformingDict, self).pop(key, *args, **kwargs)
+
+class IRCDict(KeyTransformingDict):
     """
-    Like FoldedCaseKeyedDict except uses IRCFoldedCase for the keys.
+    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()
+    ['A', '[This]']
+
+    But the keys can be referenced with a different case
     >>> d['a']
     'foo'
+
     >>> d['{this}']
     'that'
+
     >>> d['{THIS}']
     'that'
+
+    >>> '{thiS]' in d
+    True
+
+    This should work for operations like delete and pop as well.
+    >>> d.pop('A')
+    'foo'
+    >>> del d['{This}']
+    >>> len(d)
+    0
     """
     @staticmethod
     def key_transform(key):