17.1.1.1.1.2. cobra.core.dictlist
¶
Creates the class DictList, used in many parts of cobrapy.
17.1.1.1.1.2.1. Module Contents¶
17.1.1.1.1.2.1.1. Classes¶
A combined dict and list. |
-
class
cobra.core.dictlist.
DictList
(*args)[source]¶ Bases:
list
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.
-
_check
(self, id: Union[Object, str]) → None[source]¶ Make sure duplicate id’s are not added.
This function is called before adding in elements.
-
list_attr
(self, attribute: str) → list[source]¶ Return a list of the given attribute for every object.
-
get_by_any
(self, iterable: List[Union[str, Object, int]]) → list[source]¶ Get a list of members using several different ways of indexing.
-
query
(self, search_function: Union[str, Pattern, Callable], attribute: Union[str, None] = None) → 'DictList'[source]¶ Query the list.
- Parameters
search_function (a string, regular expression or 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
attribute (string or None) – the name attribute of the object to passed as argument to the search_function. If this is None, the object itself is used.
- Returns
a new list of objects which match the query
- Return type
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')
-
_replace_on_id
(self, new_object: Object) → None[source]¶ Replace an object by another with the same id.
-
union
(self, iterable: Iterable[Object]) → None[source]¶ Add elements with id’s not already in the model.
-
extend
(self, iterable: Iterable[Object]) → None[source]¶ 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.
- Parameters
iterable (Iterable) –
-
_extend_nocheck
(self, iterable: Iterable[Object]) → None[source]¶ 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.
- Parameters
iterable (Iterable) –
-
__sub__
(self, other: Iterable[Object]) → 'DictList'[source]¶ Remove a value or values, and returns the new DictList.
x.__sub__(y) <==> x - y
- Parameters
other (iterable) – other must contain only unique id’s present in the list
- Returns
total – new DictList with item(s) removed
- Return type
-
__isub__
(self, other: Iterable[Object]) → 'DictList'[source]¶ Remove a value or values in place.
x.__sub__(y) <==> x -= y
- Parameters
other (iterable) – other must contain only unique id’s present in the list
-
__add__
(self, other: Iterable[Object]) → 'DictList'[source]¶ Add item while returning a new DictList.
x.__add__(y) <==> x + y
- Parameters
other (iterable) – other must contain only unique id’s which do not intersect with self
-
__iadd__
(self, other: Iterable[Object]) → 'DictList'[source]¶ Add item while returning the same DictList.
x.__iadd__(y) <==> x += y
- Parameters
other (iterable) – other must contain only unique id’s whcih do not intersect with self
-
__reduce__
(self) → Tuple[Type['DictList'], Tuple, dict, Iterator][source]¶ 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.
-
__getstate__
(self) → dict[source]¶ 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
-
__setstate__
(self, state: dict) → None[source]¶ 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
-
index
(self, id: Union[str, Object], *args) → int[source]¶ Determine the position in the list.
- Parameters
id (A string or a
Object
) –
-
__contains__
(self, entity: Union[str, Object]) → bool[source]¶ Ask if the DictList contain an entity.
DictList.__contains__(entity) <==> entity in DictList
- Parameters
entity (str or
Object
) –
-
remove
(self, x: Union[str, Object]) → None[source]¶ 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
-
sort
(self, cmp: Callable = None, key: Callable = None, reverse: bool = False) → None[source]¶ Stable sort IN PLACE.
cmp(x, y) -> -1, 0, 1
-
__getitem__
(self, i: Union[int, slice, Iterable, Object, 'DictList']) → Union['DictList', Object][source]¶ Get item from DictList.
-
__setitem__
(self, i: Union[slice, int], y: Union[list, Object]) → None[source]¶ Set an item via index or slice.
-