3 # © Copyright 2021-2022, Scott Gasch
6 The :class:`pyutils.collectionz.bidict.BiDict` class is a subclass
7 of :py:class:`dict` that implements a bidirectional dictionary. That
8 is, it maps each key to a value in constant time and each value back
9 to the one or more keys it is associated with in constant time. It
10 does this by simply storing the data twice.
14 # Initialize with a normal dict...
15 third_party_wierdos = BiDict({
16 'prometheus-fastapi-instrumentator': 'prometheus_fastapi_instrumentator',
17 'scikit-learn': 'sklearn',
18 'antlr4-python3-runtime' : 'antlr4',
19 'python-dateutil': 'dateutil',
20 'speechrecognition': 'speech_recognition',
21 'beautifulsoup4': 'bs4',
22 'python-dateutil': 'dateutil',
23 'homeassistant-api': 'homeassistant_api',
26 # Use in one direction:
27 x = third_party_wierdos['scikit-learn']
29 # Use in opposite direction:
30 y = third_party_wierdos.inverse['python_dateutil']
32 # Note: type(y) is List since one value may map back to multiple keys.
38 def __init__(self, *args, **kwargs):
40 A class that stores both a Mapping between keys and values and
41 also the inverse mapping between values and their keys to
42 allow for efficient lookups in either direction. Because it
43 is possible to have several keys with the same value, using
44 the inverse map returns a sequence of keys.
65 super().__init__(*args, **kwargs)
67 for key, value in self.items():
68 self.inverse.setdefault(value, []).append(key)
70 def __setitem__(self, key, value):
73 self.inverse[old_value].remove(key)
74 super().__setitem__(key, value)
75 self.inverse.setdefault(value, []).append(key)
77 def __delitem__(self, key):
79 self.inverse.setdefault(value, []).remove(key)
80 if value in self.inverse and not self.inverse[value]:
81 del self.inverse[value]
82 super().__delitem__(key)
85 if __name__ == '__main__':