:py:mod:`cobra.core.dictlist` ============================= .. py:module:: cobra.core.dictlist .. autoapi-nested-parse:: Define the DictList class. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: cobra.core.dictlist.DictList .. py:class:: DictList(*args) Bases: :py:obj:`list` 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:method:: has_id(id: Union[cobra.core.object.Object, str]) -> bool Check if id is in DictList. .. py:method:: _check(id: Union[cobra.core.object.Object, 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[cobra.core.object.Object, str]) -> cobra.core.object.Object 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, cobra.core.object.Object, int]]) -> list 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 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: cobra.core.object.Object) -> None Replace an object by another with the same id. .. py:method:: append(entity: cobra.core.object.Object) -> None Append object to end. .. py:method:: union(iterable: Iterable[cobra.core.object.Object]) -> None Add elements with id's not already in the model. .. py:method:: extend(iterable: Iterable[cobra.core.object.Object]) -> 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[cobra.core.object.Object]) -> 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[cobra.core.object.Object]) -> DictList 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[cobra.core.object.Object]) -> DictList 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[cobra.core.object.Object]) -> DictList 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[cobra.core.object.Object]) -> DictList 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] 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, cobra.core.object.Object], *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, cobra.core.object.Object]) -> 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 Copy the DictList into a new one. .. py:method:: insert(index: int, entity: cobra.core.object.Object) -> None Insert entity before index. .. py:method:: pop(*args) -> cobra.core.object.Object Remove and return item at index (default last). .. py:method:: add(x: cobra.core.object.Object) -> None Opposite of `remove`. Mirrors set.add. .. py:method:: remove(x: Union[str, cobra.core.object.Object]) -> 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, cobra.core.object.Object, DictList]) -> Union[DictList, cobra.core.object.Object] Get item from DictList. .. py:method:: __setitem__(i: Union[slice, int], y: Union[list, cobra.core.object.Object]) -> 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 Get a slice from it to j of DictList. .. py:method:: __setslice__(i: int, j: int, y: Union[list, cobra.core.object.Object]) -> None Set slice, where y is an iterable. .. py:method:: __delslice__(i: int, j: int) -> None Remove slice. .. py:method:: __getattr__(attr: Any) -> Any 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