# 17.1.1.8.1.2. cobra.util.context¶

Context manager for the package.

## 17.1.1.8.1.2.1. Module Contents¶

### 17.1.1.8.1.2.1.1. Classes¶

 HistoryManager Define a base context manager.

### 17.1.1.8.1.2.1.2. Functions¶

 get_context(obj: Object) → Optional[HistoryManager] Search for a context manager. resettable(func: Callable[[Any], Any]) → 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__(self, 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(self) → None[source]

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

size(self) → int[source]

Calculate number of operations on the stack.

cobra.util.context.get_context(obj: 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
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