| 1170 | ################################################################################ |
| 1171 | |
| 1172 | class UserDict(_collections_abc.MutableMapping): |
| 1173 | |
| 1174 | # Start by filling-out the abstract methods |
| 1175 | def __init__(self, dict=None, /, **kwargs): |
| 1176 | self.data = {} |
| 1177 | if dict is not None: |
| 1178 | self.update(dict) |
| 1179 | if kwargs: |
| 1180 | self.update(kwargs) |
| 1181 | |
| 1182 | def __len__(self): |
| 1183 | return len(self.data) |
| 1184 | |
| 1185 | def __getitem__(self, key): |
| 1186 | if key in self.data: |
| 1187 | return self.data[key] |
| 1188 | if hasattr(self.__class__, "__missing__"): |
| 1189 | return self.__class__.__missing__(self, key) |
| 1190 | raise KeyError(key) |
| 1191 | |
| 1192 | def __setitem__(self, key, item): |
| 1193 | self.data[key] = item |
| 1194 | |
| 1195 | def __delitem__(self, key): |
| 1196 | del self.data[key] |
| 1197 | |
| 1198 | def __iter__(self): |
| 1199 | return iter(self.data) |
| 1200 | |
| 1201 | # Modify __contains__ and get() to work like dict |
| 1202 | # does when __missing__ is present. |
| 1203 | def __contains__(self, key): |
| 1204 | return key in self.data |
| 1205 | |
| 1206 | def get(self, key, default=None): |
| 1207 | if key in self: |
| 1208 | return self[key] |
| 1209 | return default |
| 1210 | |
| 1211 | |
| 1212 | # Now, add the methods in dicts but not in MutableMapping |
| 1213 | def __repr__(self): |
| 1214 | return repr(self.data) |
| 1215 | |
| 1216 | def __or__(self, other): |
| 1217 | if isinstance(other, UserDict): |
| 1218 | return self.__class__(self.data | other.data) |
| 1219 | if isinstance(other, (dict, frozendict)): |
| 1220 | return self.__class__(self.data | other) |
| 1221 | return NotImplemented |
| 1222 | |
| 1223 | def __ror__(self, other): |
| 1224 | if isinstance(other, UserDict): |
| 1225 | return self.__class__(other.data | self.data) |
| 1226 | if isinstance(other, (dict, frozendict)): |
| 1227 | return self.__class__(other | self.data) |
| 1228 | return NotImplemented |
| 1229 |
no outgoing calls
searching dependent graphs…