FrozenDefaultDict#
- class tqec.plaquette.FrozenDefaultDict(arg=None, *, default_value=None)[source]#
Bases:
Generic[K,V],Mapping[K,V]Implement a
defaultdictthat cannot be mutated.This class re-defines all the mutating methods of defaultdict (i.e., all the mutating methods of dict and __missing__) in order to make any instance immutable.
Note on re-defining __missing__:
The standard defaultdict implementation is entirely based on the __missing__ method (described here https://docs.python.org/3/library/collections.html#collections.defaultdict.__missing__) that is called when a user-provided key was not found in the defined keys. This __missing__ method try to use self.default_factory to create a new value and inserts that new value in the dictionary. That last part is problematic for
Plaquettesand in particular to compare collections ofPlaquettesthrough __hash__ and __eq__.- Parameters:
arg (Mapping[K, V] | Iterable[tuple[K, V]] | None) – a mapping from defined keys to values. Any key that is not present will be implicitly associated to the provided
default_value.default_value (V | None) – value that will be returned (without copying) when a key not present in
argis queried.
Methods
__init__([arg, default_value])Implement a
defaultdictthat cannot be mutated.get(k[,d])Return
Trueifselfhas a default value.items()keys()map_keys(callable)Apply
callableto each key and return a new instance with the modified keys.map_keys_if_present(mapping)Apply
callableto each key and return a new instance with the modified keys.map_values(callable)Apply
callableto each value and return a new instance with the modified values.values()Attributes
default_valueGet the default value returned when a queried key is missing.
Detailed methods
- __init__(arg=None, *, default_value=None)[source]#
Implement a
defaultdictthat cannot be mutated.This class re-defines all the mutating methods of defaultdict (i.e., all the mutating methods of dict and __missing__) in order to make any instance immutable.
Note on re-defining __missing__:
The standard defaultdict implementation is entirely based on the __missing__ method (described here https://docs.python.org/3/library/collections.html#collections.defaultdict.__missing__) that is called when a user-provided key was not found in the defined keys. This __missing__ method try to use self.default_factory to create a new value and inserts that new value in the dictionary. That last part is problematic for
Plaquettesand in particular to compare collections ofPlaquettesthrough __hash__ and __eq__.- Parameters:
arg (Mapping[K, V] | Iterable[tuple[K, V]] | None) – a mapping from defined keys to values. Any key that is not present will be implicitly associated to the provided
default_value.default_value (V | None) – value that will be returned (without copying) when a key not present in
argis queried.
- Return type:
None
- get(k[, d]) D[k] if k in D, else d. d defaults to None.#
- items() a set-like object providing a view on D's items#
- keys() a set-like object providing a view on D's keys#
- map_keys(callable)[source]#
Apply
callableto each key and return a new instance with the modified keys.- Parameters:
callable (Callable[[K], K])
- Return type:
FrozenDefaultDict[K, V]
- map_keys_if_present(mapping)[source]#
Apply
callableto each key and return a new instance with the modified keys.- Parameters:
mapping (Mapping[K, K])
- Return type:
FrozenDefaultDict[K, V]
- map_values(callable)[source]#
Apply
callableto each value and return a new instance with the modified values.- Parameters:
callable (Callable[[V], Vp])
- Return type:
FrozenDefaultDict[K, Vp]
- values() an object providing a view on D's values#