temporary_level

class tenpy.tools.optimization.temporary_level(temporary_level)[source]

Bases: object

Context manager to temporarily set the optimization level to a different value.

Parameters
temporary_levelint | OptimizationFlag | str | None

The optimization level to be set during the context. None defaults to the current value of the optimization level.

Examples

It is recommended to use this context manager in a with statement:

# optimization level default
with temporary_level(OptimizationFlag.safe):
    do_some_stuff()  # temporarily have Optimization level `safe`
    # you can even change the optimization level to something else:
    set_level(OptimizationFlag.skip_args_check)
    do_some_really_heavy_stuff()
# here we are back to the optimization level as before the ``with ...`` statement
Attributes
temporary_levelNone | OptimizationFlag

The optimization level to be set during the context.

_old_levelOptimizationFlag

Optimization level to be restored at the end of the context manager.