freezing#

Functions

freeze(k[, inplace])

Freeze a planar diagram so it cannot be modified.

frozen(*_, **__)

Dummy method used on frozen diagrams to block mutation.

lock(k[, inplace])

Lock a planar diagram so it cannot be modified.

unfreeze(k[, inplace])

Unfreeze a planar diagram so it can be modified again.

freeze(k, inplace=True)#

Freeze a planar diagram so it cannot be modified.

Freezing monkey-patches mutating methods on the instance to a dummy method that raises RuntimeError. The diagram’s data stays intact.

Parameters:
Returns:

PlanarDiagram – The frozen diagram (k or its copy).

Return type:

PlanarDiagram

Examples

>>> from knotpy.classes.planardiagram import PlanarDiagram
>>> d = PlanarDiagram()
>>> d.add_vertices_from(["a", "b"])
>>> freeze(d)
PlanarDiagram(...)
>>> d.is_frozen()
True
>>> d.add_vertex("c")
Traceback (most recent call last):
    ...
RuntimeError: Frozen diagrams cannot be modified
unfreeze(k, inplace=True)#

Unfreeze a planar diagram so it can be modified again.

Restores original bound methods from the diagram’s class. Useful when a frozen diagram was used as a dict/set key and you want to mutate it again.

Parameters:
Returns:

PlanarDiagram – The unfrozen diagram (k or its copy).

Return type:

PlanarDiagram

Examples

>>> from knotpy.classes.planardiagram import PlanarDiagram
>>> d = PlanarDiagram()
>>> freeze(d)
PlanarDiagram(...)
>>> unfreeze(d)
PlanarDiagram(...)
>>> d.is_frozen()
False
>>> d.add_vertex("x")  # works again
lock(k, inplace=True)#

Lock a planar diagram so it cannot be modified. Unlike freezing, a diagram cannot be unlocked. Used for knot tables, so the user cannot modify a knot in a precomputed knot table.

Parameters:
Return type:

PlanarDiagram