BiMap#
- class hugr.utils.BiMap(fwd: Mapping[L, R] | None = None)[source]#
Bases:
MutableMapping
,Generic
[L
,R
]Bidirectional map backed by two dictionaries, between left types L and right types R.
Methods
Delete a left key and its right value.
Delete a right key and its left value.
Get a left value using a right key.
Get a right value using a left key.
Insert a left key and right value.
Insert a right key and left value.
Iterator over left, right pairs.
If key is not found, d is returned if given, otherwise KeyError is raised.
as a 2-tuple; but raise KeyError if D is empty.
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
Attributes
fwd
bck
- clear() None. Remove all items from D. #
- delete_left(key: L) None [source]#
Delete a left key and its right value.
- Parameters:
key – Left key.
- Raises:
KeyError – If the key is not found.
Example
>>> bm = BiMap({"a": 1}) >>> bm.delete_left("a") >>> bm BiMap({})
- delete_right(key: R) None [source]#
Delete a right key and its left value.
- Parameters:
key – Right key.
- Raises:
KeyError – If the key is not found.
Example
>>> bm = BiMap({"a": 1}) >>> bm.delete_right(1) >>> bm BiMap({})
- get(k[, d]) D[k] if k in D, else d. d defaults to None. #
- get_left(key: R) L | None [source]#
Get a left value using a right key.
Example
>>> bm = BiMap({"a": 1}) >>> bm.get_left(1) 'a' >>> bm.get_left(2)
- get_right(key: L) R | None [source]#
Get a right value using a left key.
Example
>>> bm = BiMap({"a": 1}) >>> bm.get_right("a") 1 >>> bm.get_right("b")
- insert_left(key: L, value: R) None [source]#
Insert a left key and right value. If the key or value already exist, the existing key-value pair is replaced.
- Parameters:
key – Left key.
value – Right value.
Example
>>> bm = BiMap() >>> bm.insert_left("a", 1) >>> bm["a"] 1
- insert_right(key: R, value: L) None [source]#
Insert a right key and left value. If the key or value already exist, the existing key-value pair is replaced.
- Parameters:
key – Right key.
value – Left value.
Example
>>> bm = BiMap() >>> bm.insert_right(1, "a") >>> bm["a"] 1
- items() ItemsView[L, R] [source]#
Iterator over left, right pairs.
Example
>>> bm = BiMap({"a": 1, "b": 2}) >>> list(bm.items()) [('a', 1), ('b', 2)]
- keys() a set-like object providing a view on D's keys #
- pop(k[, d]) v, remove specified key and return the corresponding value. #
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair #
as a 2-tuple; but raise KeyError if D is empty.
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D #
- update([E, ]**F) None. Update D from mapping/iterable E and F. #
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values #