cobra.util.context
==================

.. py:module:: cobra.util.context

.. autoapi-nested-parse::

   Context manager for the package.



Classes
-------

.. autoapisummary::

   cobra.util.context.HistoryManager


Functions
---------

.. autoapisummary::

   cobra.util.context.get_context
   cobra.util.context.resettable


Module Contents
---------------

.. py:class:: HistoryManager(**kwargs)

   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`.



   .. py:attribute:: _history
      :value: []



   .. py:method:: __call__(operation: Callable[[Any], Any]) -> None

      Add the corresponding operation to the history stack.

      :param operation: A function to be called at a later time.
      :type operation: callable



   .. py:method:: reset() -> None

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



   .. py:method:: size() -> int

      Calculate number of operations on the stack.



.. py:function:: get_context(obj: cobra.Object) -> Optional[HistoryManager]

   Search for a context manager.

   :param obj: The cobra.Object for which to search context manager.
   :type obj: cobra.Object

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

   :raises AttributeError: If no context manager is found.
   :raises IndexError: If no context manager is found.


.. py:function:: resettable(func: Callable[[Any], Any]) -> Callable[[Any], Any]

   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`.

   :param func: The function to decorate.
   :type func: callable

   :returns: The decorated function.
   :rtype: callable


