cobra.util.context

Context manager for the package.

Module Contents

Classes

HistoryManager

Define a base context manager.

Functions

get_context(→ Optional[HistoryManager])

Search for a context manager.

resettable(→ Callable[[Any], Any])

Simplify the context management of simple object attributes.

class cobra.util.context.HistoryManager(**kwargs)[source]

Define a base context manager.

It records a list of actions to be taken at a later time. This is used to implement context managers that allow temporary changes to a cobra.core.Model.

__call__(operation: Callable[[Any], Any]) None[source]

Add the corresponding operation to the history stack.

Parameters

operation (callable) – A function to be called at a later time.

reset() None[source]

Trigger executions for all items in the stack in reverse order.

size() int[source]

Calculate number of operations on the stack.

cobra.util.context.get_context(obj: cobra.Object) Optional[HistoryManager][source]

Search for a context manager.

Parameters

obj (cobra.Object) – The cobra.Object for which to search context manager.

Returns

HistoryManager instance, or None if no context manager is found.

Return type

HistoryManager or None

Raises
cobra.util.context.resettable(func: Callable[[Any], Any]) Callable[[Any], Any][source]

Simplify the context management of simple object attributes.

It gets the value of the attribute prior to setting it, and stores a function to set the value to the old value in the cobra.util.HistoryManager.

Parameters

func (callable) – The function to decorate.

Returns

The decorated function.

Return type

callable