lightbulb.di.graph¶
- class DependencyData(factory_method: Callable[..., types.MaybeAwaitable[T]], factory_params: Mapping[str, DependencyExpression[T]], teardown_method: Callable[[T], types.MaybeAwaitable[None]] | None)[source]¶
Data required in order to be able to create/destroy a given dependency.
- Parameters:
factory_method – The method used to create the dependency.
factory_params – Mapping of param name to dependency expression for any dependencies the factory depends on.
teardown_method – The optional method used to teardown the dependency.
- factory_method: Callable[..., types.MaybeAwaitable[T]]¶
The method used to create the dependency.
- factory_params: Mapping[str, DependencyExpression[T]]¶
Mapping of param name to dependency expression for any dependencies the factory depends on.
- class DiGraph(initial: DiGraph | None = None)[source]¶
Implementation of a directional graph datastructure for use as a dependency graph.
- Parameters:
initial – The initial graph to use to populate the starting state of the graph. Defaults to
None
.
- add_edge(from_: str, to_: str, /) None [source]¶
Add an edge to the graph. Fails if either the origin or destination nodes are not in the graph.
- Parameters:
from – The origin node.
to – The destination node.
- Returns:
- Raises:
ValueError – If either the origin or destination nodes are not in the graph.
- add_node(id_: str, /, data: DependencyData[Any] | None) None [source]¶
Add a node to the graph.
- Parameters:
id – The ID of the node to add.
data – The data for the node.
- Returns:
- children(of: str, /) Set[str] [source]¶
Get the set of all children for the given node. Includes indirect children where a node depends on a node that depends on the requested node (etc.).
- Parameters:
of – The node to get the children for.
- Returns:
Set of all children for the given node.
- in_edges(id_: str, /) Set[tuple[str, str]] [source]¶
Get the in edges for the node with the given dependency ID. In the context of DI, the edges represent the dependencies that depend on the requested dependency.
- Parameters:
id – The ID of the dependency to get in edges for.
- Returns:
The in edges for the node with the given dependency ID.
- out_edges(id_: str, /) Set[tuple[str, str]] [source]¶
Get the out edges for the node with the given dependency ID. In the context of DI, the edges represent the dependencies that the requested dependency directly depends on.
- Parameters:
id – The ID of the dependency to get out edges for.
- Returns:
The out edges for the node with the given dependency ID.
- remove_edge(from_: str, to_: str, /) None [source]¶
Remove an edge from the graph. Does nothing if either the origin or destination nodes are not in the graph.
- Parameters:
from – The origin node.
to – The destination node.
- Returns:
- remove_node(id_: str, /) None [source]¶
Remove a node from the graph. Does nothing if the node is not present in the graph. Also removes all edges referencing the node.
- Parameters:
id – The ID of the node to remove.
- Returns:
- replace_node(id_: str, /, data: DependencyData[Any] | None) None [source]¶
Replace the data for the node with the given ID. Preserves all edges already referencing this node. Adds the node if it is not already in the graph.
- Parameters:
id – The ID of the node to replace.
data – The data for the node.
- Returns:
- subgraph(of: Collection[str], /) DiGraph [source]¶
Create a subgraph containing only the given nodes, and any edges relating them. The created graph will only contain the requested nodes, and no nodes that depend on any of the given nodes that were not specified.
- Parameters:
of – The nodes the subgraph should contain.
- Returns:
The created subgraph.
- property edges: Set[tuple[str, str]]¶
Set containing all edges within this graph. An edge is represented by a tuple where the first element is the origin node, and the second element is the destination node.
- property nodes: Mapping[str, DependencyData[t.Any] | None]¶
Mapping of dependency ID to the data for that dependency. If the data is
None
, it indicates that the node was added indirectly and that the dependency for that ID has not been directly registered to this graph.