turbograph.backend subpackage#

Package that defines the networkx and igraph graph backends.

The igraph and networkx backends are defined in turbograph.backend.igraph_backend and turbograph.backend.networkx_backend, respectively.

To use a specific backend, import the corresponding wrapper class using the turbograph.backend.factory.get_graph_backend() function.

turbograph.backend.igraph_backend module#

Graph backend implementation using the py:mod:igraph library.

This module provides a concrete implementation of the GraphWrapper interface using igraph.Graph.

class turbograph.backend.igraph_backend.IGraphWrapper(graph=None)[source]#

Bases: GraphWrapper[Graph, V]

Graph backend for the igraph library.

Parameters:

graph (G | None)

classmethod initialize_empty()[source]#

Initialize and return an empty graph.

Return type:

Graph

get_graph_copy()[source]#

Return a copy of the internal graph.

Return type:

Graph

add_vertex(vertex, **attributes)[source]#

Add a vertex with specified attributes to the graph.

Return type:

None

Parameters:
  • vertex (V) – The vertex to add.

  • **attributes (Unpack[VertexAttributes[V]]) – Keyword arguments representing the vertex attributes.

add_edge(source, target)[source]#

Add an edge between the source and target vertices.

Return type:

None

Parameters:
  • source (V) – The source vertex.

  • target (V) – The target vertex.

delete_vertex(*vertices)[source]#

Delete specified vertices from the graph.

Return type:

None

Parameters:

*vertices (V) – The vertices to delete.

Raises:

VertexError – If a vertex is not found in the graph.

delete_edge(source, target)[source]#

Delete an edge between the source and target vertices.

Return type:

None

Parameters:
  • source (V) – The source vertex.

  • target (V) – The target vertex.

Raises:

EdgeError – If the edge is not found in the graph.

get_vertex_attribute(vertex, key)[source]#

Get the value of a specific attribute of a vertex.

Return type:

object

Parameters:
  • vertex (V) – The vertex whose attribute is to be retrieved.

  • key (Literal['func', 'value', 'predecessors']) – The attribute key.

Raises:

VertexError – If the vertex is not found in the graph.

get_vertex_attributes(vertex)[source]#

Get all attributes of a vertex.

Return type:

VertexAttributes[TypeVar(V, bound= Hashable)]

Parameters:

vertex (V) – The vertex whose attributes are to be retrieved.

Raises:

VertexError – If the vertex is not found in the graph.

set_vertex_attribute(vertex, key, value)[source]#

Set the value of a specific attribute of a vertex.

Return type:

None

Parameters:
  • vertex (V) – The vertex whose attribute is to be set.

  • key (Literal['func', 'value', 'predecessors']) – The attribute key.

  • value (object) – The value to set.

Raises:

VertexError – If the vertex is not found in the graph.

update_vertex_attributes(vertex, attributes)[source]#

Update multiple attributes of a vertex.

Return type:

None

Parameters:
  • vertex (V) – The vertex whose attributes are to be updated.

  • attributes (Mapping[Literal['func', 'value', 'predecessors'], ~typing.Any]) – A mapping of attribute keys to values.

Raises:

VertexError – If the vertex is not found in the graph.

property call_mode: Literal['args', 'kwargs', 'arg'] | None#

Return the current call mode of the graph.

get_neighbors(vertex, direction='all')[source]#

Get the neighbors of a vertex in the specified direction.

Return type:

Iterable[TypeVar(V, bound= Hashable)]

Parameters:
  • vertex (V) – The vertex whose neighbors are to be retrieved.

  • direction (Literal['in', 'out', 'all']) – The direction of the edges to consider.

Raises:

VertexError – If the vertex is not found in the graph.

get_degree(vertex, direction='all')[source]#

Get the degree of a vertex in the specified direction.

Return type:

int

Parameters:
  • vertex (V) – The vertex whose degree is to be retrieved.

  • direction (Literal['in', 'out', 'all']) – The direction of the edges to consider.

Raises:

VertexError – If the vertex is not found in the graph.

get_subcomponent(vertex, direction='all')[source]#

Get the subcomponent of a vertex in the specified direction.

Return type:

Iterable[TypeVar(V, bound= Hashable)]

Parameters:
  • vertex (V) – The vertex whose subcomponent is to be retrieved.

  • direction (Literal['in', 'out', 'all']) – The direction of the edges to consider

Raises:

VertexError – If the vertex is not found in the graph.

is_dag()[source]#

Check if the graph is a directed acyclic graph (DAG).

Return type:

bool

reset()[source]#

Reset the graph while preserving its structure.

This method clears the stored functions and values from all vertices while preserving the graph’s structure and dependencies.

Return type:

None

get_sorted_vertices(direction)[source]#

Get the vertices sorted topologically in the specified direction.

Return type:

Iterable[TypeVar(V, bound= Hashable)]

Parameters:

direction (Literal['in', 'out']) – The direction of the edges to consider.

turbograph.backend.networkx_backend module#

Graph backend implementation using the networkx library.

This module provides a concrete implementation of the GraphWrapper interface using networkx.DiGraph.

class turbograph.backend.networkx_backend.NetworkXWrapper(graph=None)[source]#

Bases: GraphWrapper[_DiGraph[V], V]

Graph backend for the networkx library using a directed graph (DiGraph).

Parameters:

graph (G | None)

classmethod initialize_empty()[source]#

Initialize and return an empty graph.

Return type:

_DiGraph[TypeVar(V, bound= Hashable)]

get_graph_copy()[source]#

Return a copy of the internal graph.

Return type:

_DiGraph

add_vertex(vertex, **attributes)[source]#

Add a vertex with specified attributes to the graph.

Return type:

None

Parameters:
  • vertex (V) – The vertex to add.

  • **attributes (Any) – Keyword arguments representing the vertex attributes.

add_edge(source, target)[source]#

Add an edge between the source and target vertices.

Return type:

None

Parameters:
  • source (V) – The source vertex.

  • target (V) – The target vertex.

delete_vertex(*vertices)[source]#

Delete specified vertices from the graph.

Return type:

None

Parameters:

*vertices (V) – The vertices to delete.

Raises:

VertexError – If a vertex is not found in the graph.

delete_edge(source, target)[source]#

Delete an edge between the source and target vertices.

Return type:

None

Parameters:
  • source (V) – The source vertex.

  • target (V) – The target vertex.

Raises:

EdgeError – If the edge is not found in the graph.

get_vertex_attribute(vertex, key)[source]#

Get the value of a specific attribute of a vertex.

Return type:

object

Parameters:
  • vertex (V) – The vertex whose attribute is to be retrieved.

  • key (Literal['func', 'value', 'predecessors']) – The attribute key.

Raises:

VertexError – If the vertex is not found in the graph.

get_vertex_attributes(vertex)[source]#

Get all attributes of a vertex.

Return type:

VertexAttributes[TypeVar(V, bound= Hashable)]

Parameters:

vertex (V) – The vertex whose attributes are to be retrieved.

Raises:

VertexError – If the vertex is not found in the graph.

set_vertex_attribute(vertex, key, value)[source]#

Set the value of a specific attribute of a vertex.

Return type:

None

Parameters:
  • vertex (V) – The vertex whose attribute is to be set.

  • key (Literal['func', 'value', 'predecessors']) – The attribute key.

  • value (object) – The value to set.

Raises:

VertexError – If the vertex is not found in the graph.

update_vertex_attributes(vertex, attributes)[source]#

Update multiple attributes of a vertex.

Return type:

None

Parameters:
  • vertex (V) – The vertex whose attributes are to be updated.

  • attributes (Mapping[Literal['func', 'value', 'predecessors'], ~typing.Any]) – A mapping of attribute keys to values.

Raises:

VertexError – If the vertex is not found in the graph.

property call_mode: Literal['args', 'kwargs', 'arg'] | None#

Return the current call mode of the graph.

get_neighbors(vertex, direction='all')[source]#

Get the neighbors of a vertex in the specified direction.

Return type:

Iterable[TypeVar(V, bound= Hashable)]

Parameters:
  • vertex (V) – The vertex whose neighbors are to be retrieved.

  • direction (Literal['in', 'out', 'all']) – The direction of the edges to consider.

Raises:

VertexError – If the vertex is not found in the graph.

get_degree(vertex, direction='all')[source]#

Get the degree of a vertex in the specified direction.

Return type:

int

Parameters:
  • vertex (V) – The vertex whose degree is to be retrieved.

  • direction (Literal['in', 'out', 'all']) – The direction of the edges to consider.

Raises:

VertexError – If the vertex is not found in the graph.

get_subcomponent(vertex, direction='all')[source]#

Get the subcomponent of a vertex in the specified direction.

Return type:

Iterable[TypeVar(V, bound= Hashable)]

Parameters:
  • vertex (V) – The vertex whose subcomponent is to be retrieved.

  • direction (Literal['in', 'out', 'all']) – The direction of the edges to consider

Raises:

VertexError – If the vertex is not found in the graph.

is_dag()[source]#

Check if the graph is a directed acyclic graph (DAG).

Return type:

bool

get_sorted_vertices(direction)[source]#

Get the vertices sorted topologically in the specified direction.

Return type:

Iterable[TypeVar(V, bound= Hashable)]

Parameters:

direction (Literal['in', 'out']) – The direction of the edges to consider.

turbograph.backend.factory module#

Factory module for selecting and managing graph backends.

This module provides functions to:

  • Check the availability of graph backends (networkx and igraph).

  • Automatically determine the best available backend.

  • Retrieve the appropriate backend wrapper class for graph operations.

turbograph.backend.factory.Backend#

Enumeration of supported graph backends.

alias of Literal[‘networkx’, ‘igraph’]

turbograph.backend.factory.BACKENDS: tuple[Literal['networkx', 'igraph'], ...] = ('networkx', 'igraph')#

Tuple of available graph backends.

The first available backend is selected as the default unless explicitly specified.

turbograph.backend.factory.backend_to_module: dict[Literal['networkx', 'igraph'], str] = {'igraph': 'igraph', 'networkx': 'networkx'}#

Mapping of backend names to their corresponding module names.

This mapping is used to dynamically check the availability of backend libraries.

turbograph.backend.factory.get_backend_auto()[source]#

Automatically select the best available graph backend.

Return type:

Literal['networkx', 'igraph']

Returns:

The first available backend from the predefined list (BACKENDS).

Raises:

ImportError – If no supported backend is installed.

Example

>>> get_backend_auto()
'networkx'  # If 'networkx' is installed
turbograph.backend.factory.get_graph_backend(backend=None)[source]#

Retrieve the appropriate backend wrapper class for graph operations.

Return type:

type[Union[NetworkXWrapper[Any], IGraphWrapper[Any]]]

Parameters:

backend (Literal['networkx', 'igraph'] | None) – The backend to use. If None, the best available backend is selected automatically using get_backend_auto().

Returns:

The backend wrapper class.

Raises:
  • ValueError – If an unrecognized backend name is provided.

  • ImportError – If no supported backend is installed.