dict.py 940 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import unicode_literals, absolute_import
  2. import six
  3. from jaraco.collections import KeyTransformingDict
  4. from . import strings
  5. class IRCDict(KeyTransformingDict):
  6. """
  7. A dictionary of names whose keys are case-insensitive according to the
  8. IRC RFC rules.
  9. >>> d = IRCDict({'[This]': 'that'}, A='foo')
  10. The dict maintains the original case:
  11. >>> '[This]' in ''.join(d.keys())
  12. True
  13. But the keys can be referenced with a different case
  14. >>> d['a'] == 'foo'
  15. True
  16. >>> d['{this}'] == 'that'
  17. True
  18. >>> d['{THIS}'] == 'that'
  19. True
  20. >>> '{thiS]' in d
  21. True
  22. This should work for operations like delete and pop as well.
  23. >>> d.pop('A') == 'foo'
  24. True
  25. >>> del d['{This}']
  26. >>> len(d)
  27. 0
  28. """
  29. @staticmethod
  30. def transform_key(key):
  31. if isinstance(key, six.string_types):
  32. key = strings.IRCFoldedCase(key)
  33. return key