|
| 1 | +""" |
| 2 | +Semantics of ``__missing__`` across mappings. |
| 3 | +
|
| 4 | +✅ = indicates ``__missing__`` was called |
| 5 | +
|
| 6 | +Subclass of ``dict``:: |
| 7 | +
|
| 8 | + >>> d = DictSub(A = 'letter A') |
| 9 | + >>> d['a'] # ✅ |
| 10 | + 'letter A' |
| 11 | + >>> d.get('a', '') |
| 12 | + '' |
| 13 | + >>> 'a' in d |
| 14 | + False |
| 15 | +
|
| 16 | +Subclass of ``UserDict``:: |
| 17 | +
|
| 18 | + >>> ud = UserDictSub(A = 'letter A') |
| 19 | + >>> ud['a'] # ✅ |
| 20 | + 'letter A' |
| 21 | + >>> ud.get('a', '') # ✅ |
| 22 | + 'letter A' |
| 23 | + >>> 'a' in ud |
| 24 | + False |
| 25 | +
|
| 26 | +
|
| 27 | +Simple subclass of ``abc.Mapping``:: |
| 28 | +
|
| 29 | + >>> sms = SimpleMappingSub(A = 'letter A') |
| 30 | + >>> sms['a'] |
| 31 | + Traceback (most recent call last): |
| 32 | + ... |
| 33 | + KeyError: 'a' |
| 34 | + >>> sms.get('a', '') |
| 35 | + '' |
| 36 | + >>> 'a' in sms |
| 37 | + False |
| 38 | +
|
| 39 | +
|
| 40 | +Subclass of ``abc.Mapping`` with support for ``__missing__``:: |
| 41 | +
|
| 42 | + >>> mms = MappingMissingSub(A = 'letter A') |
| 43 | + >>> mms['a'] # ✅ |
| 44 | + 'letter A' |
| 45 | + >>> mms.get('a', '') # ✅ |
| 46 | + 'letter A' |
| 47 | + >>> 'a' in mms # ✅ |
| 48 | + True |
| 49 | +
|
| 50 | +Subclass of ``abc.Mapping`` with support for ``__missing__``:: |
| 51 | +
|
| 52 | + >>> dms = DictLikeMappingSub(A = 'letter A') |
| 53 | + >>> dms['a'] # ✅ |
| 54 | + 'letter A' |
| 55 | + >>> dms.get('a', '') |
| 56 | + '' |
| 57 | + >>> 'a' in dms |
| 58 | + False |
| 59 | +
|
| 60 | +
|
| 61 | +""" |
| 62 | + |
| 63 | +from collections import UserDict |
| 64 | +from collections import abc |
| 65 | + |
| 66 | + |
| 67 | +def _upper(x): |
| 68 | + try: |
| 69 | + return x.upper() |
| 70 | + except AttributeError: |
| 71 | + return x |
| 72 | + |
| 73 | + |
| 74 | +class DictSub(dict): |
| 75 | + def __missing__(self, key): |
| 76 | + return self[_upper(key)] |
| 77 | + |
| 78 | + |
| 79 | +class UserDictSub(UserDict): |
| 80 | + def __missing__(self, key): |
| 81 | + return self[_upper(key)] |
| 82 | + |
| 83 | + |
| 84 | +class SimpleMappingSub(abc.Mapping): |
| 85 | + def __init__(self, *args, **kwargs): |
| 86 | + self._data = dict(*args, **kwargs) |
| 87 | + |
| 88 | + # next three methods: abstract in ABC |
| 89 | + def __getitem__(self, key): |
| 90 | + return self._data[key] |
| 91 | + |
| 92 | + def __len__(self): |
| 93 | + return len(self._data) |
| 94 | + |
| 95 | + def __iter__(self): |
| 96 | + return iter(self._data) |
| 97 | + |
| 98 | + # never called by instances of this class |
| 99 | + def __missing__(self, key): |
| 100 | + return self[_upper(key)] |
| 101 | + |
| 102 | + |
| 103 | +class MappingMissingSub(SimpleMappingSub): |
| 104 | + def __getitem__(self, key): |
| 105 | + try: |
| 106 | + return self._data[key] |
| 107 | + except KeyError: |
| 108 | + return self[_upper(key)] |
| 109 | + |
| 110 | + |
| 111 | +class DictLikeMappingSub(SimpleMappingSub): |
| 112 | + def __getitem__(self, key): |
| 113 | + try: |
| 114 | + return self._data[key] |
| 115 | + except KeyError: |
| 116 | + return self[_upper(key)] |
| 117 | + |
| 118 | + def get(self, key, default=None): |
| 119 | + return self._data.get(key, default) |
| 120 | + |
| 121 | + def __contains__(self, key): |
| 122 | + return key in self._data |
0 commit comments