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

DictList

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.

has_id(self, id: Union[Object, str]) → bool[source]

Check if id is in DictList.

_check(self, id: Union[Object, str]) → None[source]

Make sure duplicate id’s are not added.

This function is called before adding in elements.

_generate_index(self) → None[source]

Rebuild the _dict index.

get_by_id(self, id: Union[Object, str]) → Object[source]

Return the element with a matching id.

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.

Parameters

iterable (list (if not, turned into single element list)) – 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

Returns

a list of members

Return type

list

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

DictList

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.

append(self, entity: Object) → None[source]

Append object to end.

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

DictList

__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) –

__copy__(self) → 'DictList'[source]

Copy the DictList into a new one.

insert(self, index: int, entity: Object) → None[source]

Insert entity before index.

pop(self, *args) → Object[source]

Remove and return item at index (default last).

add(self, x: Object) → None[source]

Opposite of remove. Mirrors set.add.

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

reverse(self) → None[source]

Reverse IN PLACE.

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.

Parameters
  • i (slice, int) – i can be slice or int. If i is a slice, y needs to be a list

  • y (list, Object) – Object to set as

__delitem__(self, index: int) → None[source]

Remove item from DictList.

__getslice__(self, i: int, j: int) → 'DictList'[source]

Get a slice from it to j of DictList.

__setslice__(self, i: int, j: int, y: Union[list, Object]) → None[source]

Set slice, where y is an iterable.

__delslice__(self, i: int, j: int) → None[source]

Remove slice.

__getattr__(self, attr: Any) → Any[source]

Get an attribute by id.

__dir__(self) → list[source]

Directory of the DictList.

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

Returns

attributes – A list of attributes/entities.

Return type

list