FrozenDefaultDict#

class tqec.plaquette.FrozenDefaultDict(arg=None, *, default_value=None)[source]#

Bases: Generic[K, V], Mapping[K, V]

A defaultdict implementation that 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 Plaquettes and in particular to compare collections of Plaquettes through __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 arg is queried.

Methods

__init__([arg, default_value])

A defaultdict implementation that cannot be mutated.

get(k[,d])

has_default_value()

Return True if self has a default value.

items()

keys()

map_keys(callable)

Apply callable to each key and return a new instance with the modified keys.

map_keys_if_present(mapping)

Apply callable to each key and return a new instance with the modified keys.

map_values(callable)

Apply callable to each value and return a new instance with the modified values.

values()

Attributes

default_value

Get the default value returned when a queried key is missing.

Detailed methods

__init__(arg=None, *, default_value=None)[source]#

A defaultdict implementation that 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 Plaquettes and in particular to compare collections of Plaquettes through __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 arg is queried.

Return type:

None

get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
has_default_value()[source]#

Return True if self has a default value.

Return type:

bool

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 callable to 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 callable to 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 callable to 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#