cobra.core.dictlist
===================

.. py:module:: cobra.core.dictlist

.. autoapi-nested-parse::

   Define the DictList class.



Attributes
----------

.. autoapisummary::

   cobra.core.dictlist.CobraObject


Classes
-------

.. autoapisummary::

   cobra.core.dictlist.DictList


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

.. py:data:: CobraObject

.. py:class:: DictList(*args)

   Bases: :py:obj:`List`\ [\ :py:obj:`CobraObject`\ ]


   Define a combined dict and list.

   This object behaves like a list, but has the O(1) speed
   benefits of a dict when looking up elements by their id.



   .. py:attribute:: _dict


   .. py:method:: has_id(id: Union[CobraObject, str]) -> bool

      Check if id is in DictList.



   .. py:method:: _check(id: Union[CobraObject, str]) -> None

      Make sure duplicate id's are not added.

      This function is called before adding in elements.




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

      Rebuild the _dict index.



   .. py:method:: get_by_id(id: Union[CobraObject, str]) -> CobraObject

      Return the element with a matching id.



   .. py:method:: list_attr(attribute: str) -> list

      Return a list of the given attribute for every object.



   .. py:method:: get_by_any(iterable: List[Union[str, CobraObject, int]]) -> List[CobraObject]

      Get a list of members using several different ways of indexing.

      :param iterable: list where each element is either int (referring to an index in
                       in this DictList), string (a id of a member in this DictList) or
                       member of this DictList for pass-through
      :type iterable: list (if not, turned into single element list)

      :returns: a list of members
      :rtype: list



   .. py:method:: query(search_function: Union[str, Pattern, Callable], attribute: Union[str, None] = None) -> DictList[CobraObject]

      Query the list.

      :param search_function: Used to find the matching elements in the list.
                              - a regular expression (possibly compiled), in which case the
                              given attribute of the object should match the regular expression.
                              - a function which takes one argument and returns True for
                              desired values
      :type search_function: a string, regular expression or function
      :param attribute: the name attribute of the object to passed as argument to the
                        `search_function`. If this is None, the object itself is used.
      :type attribute: string or None

      :returns: a new list of objects which match the query
      :rtype: DictList

      .. rubric:: Examples

      >>> from cobra.io import load_model
      >>> model = load_model('textbook')
      >>> model.reactions.query(lambda x: x.boundary)
      >>> import re
      >>> regex = re.compile('^g', flags=re.IGNORECASE)
      >>> model.metabolites.query(regex, attribute='name')



   .. py:method:: _replace_on_id(new_object: CobraObject) -> None

      Replace an object by another with the same id.



   .. py:method:: append(entity: CobraObject) -> None

      Append object to end.



   .. py:method:: union(iterable: Iterable[CobraObject]) -> None

      Add elements with id's not already in the model.



   .. py:method:: extend(iterable: Iterable[CobraObject]) -> None

      Extend list by appending elements from the iterable.

      Sometimes during initialization from an older pickle, _dict
      will not have initialized yet, because the initialization class was
      left unspecified. This is an issue because unpickling calls
      DictList.extend, which requires the presence of _dict. Therefore,
      the issue is caught and addressed here.

      :param iterable:
      :type iterable: Iterable



   .. py:method:: _extend_nocheck(iterable: Iterable[CobraObject]) -> None

      Extend without checking for uniqueness.

      This function should only be used internally by DictList when it
      can guarantee elements are already unique (as in when coming from
      self or other DictList). It will be faster because it skips these
      checks.

      :param iterable:
      :type iterable: Iterable



   .. py:method:: __sub__(other: Iterable[CobraObject]) -> DictList[CobraObject]

      Remove a value or values, and returns the new DictList.

      x.__sub__(y) <==> x - y

      :param other: other must contain only unique id's present in the list
      :type other: iterable

      :returns: **total** -- new DictList with item(s) removed
      :rtype: DictList



   .. py:method:: __isub__(other: Iterable[CobraObject]) -> DictList[CobraObject]

      Remove a value or values in place.

      x.__sub__(y) <==> x -= y

      :param other: other must contain only unique id's present in the list
      :type other: iterable



   .. py:method:: __add__(other: Iterable[CobraObject]) -> DictList[CobraObject]

      Add item while returning a new DictList.

      x.__add__(y) <==> x + y

      :param other: other must contain only unique id's which do not intersect
                    with self
      :type other: iterable



   .. py:method:: __iadd__(other: Iterable[CobraObject]) -> DictList[CobraObject]

      Add item while returning the same DictList.

      x.__iadd__(y) <==> x += y

      :param other: other must contain only unique id's whcih do not intersect
                    with self
      :type other: iterable



   .. py:method:: __reduce__() -> Tuple[Type[DictList], Tuple, dict, Iterator[CobraObject]]

      Return a reduced version of DictList.

      This reduced version details the class, an empty Tuple, a dictionary of the
      state and an iterator to go over the DictList.



   .. py:method:: __getstate__() -> dict

      Get internal state.

      This is only provided for backwards compatibility so older
      versions of cobrapy can load pickles generated with cobrapy. In
      reality, the "_dict" state is ignored when loading a pickle



   .. py:method:: __setstate__(state: dict) -> None

      Pretend to set internal state. Actually recalculates.

      Ignore the passed in state and recalculate it. This is only for
      compatibility with older pickles which did not correctly specify
      the initialization class



   .. py:method:: index(id: Union[str, CobraObject], *args) -> int

      Determine the position in the list.

      :param id:
      :type id: A string or a :class:`~cobra.core.Object.Object`



   .. py:method:: __contains__(entity: Union[str, CobraObject]) -> bool

      Ask if the DictList contain an entity.

      DictList.__contains__(entity) <==> entity in DictList

      :param entity:
      :type entity: str or :class:`~cobra.core.Object.Object`



   .. py:method:: __copy__() -> DictList[CobraObject]

      Copy the DictList into a new one.



   .. py:method:: insert(index: int, entity: CobraObject) -> None

      Insert entity before index.



   .. py:method:: pop(*args) -> CobraObject

      Remove and return item at index (default last).



   .. py:method:: add(x: CobraObject) -> None

      Opposite of `remove`. Mirrors set.add.



   .. py:method:: remove(x: Union[str, CobraObject]) -> None

      .. warning :: Internal use only.

      Each item is unique in the list which allows this
      It is much faster to do a dict lookup than n string comparisons



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

      Reverse *IN PLACE*.



   .. py:method:: sort(cmp: Callable = None, key: Callable = None, reverse: bool = False) -> None

      Stable sort *IN PLACE*.

      cmp(x, y) -> -1, 0, 1




   .. py:method:: __getitem__(i: Union[int, slice, Iterable, CobraObject, DictList[CobraObject]]) -> Union[DictList[CobraObject], CobraObject]

      Get item from DictList.



   .. py:method:: __setitem__(i: Union[slice, int], y: Union[List[CobraObject], CobraObject]) -> None

      Set an item via index or slice.

      :param i: i can be slice or int. If i is a slice, y needs to be a list
      :type i: slice, int
      :param y: Object to set as
      :type y: list, Object



   .. py:method:: __delitem__(index: int) -> None

      Remove item from DictList.



   .. py:method:: __getslice__(i: int, j: int) -> DictList[CobraObject]

      Get a slice from it to j of DictList.



   .. py:method:: __setslice__(i: int, j: int, y: Union[List[CobraObject], CobraObject]) -> None

      Set slice, where y is an iterable.



   .. py:method:: __delslice__(i: int, j: int) -> None

      Remove slice.



   .. py:method:: __getattr__(attr: Any) -> CobraObject

      Get an attribute by id.



   .. py:method:: __dir__() -> list

      Directory of the DictList.

      Override this to allow tab complete of items by their id.

      :returns: **attributes** -- A list of attributes/entities.
      :rtype: list



