TierkreisGraph#
- class tierkreis.core.tierkreis_graph.TierkreisGraph(name: str = '')[source]#
Bases:
object
Python in-memory representation of a Tierkreis graph. Can be loaded from and written to protobuf format. Supports building graphs programmatically.
Methods
Add a box node by specifying the inner graph and the inputs to connect to the node.
Add a constant value to the graph.
Add an edge to the graph, connecting output port to input port.
Add a function call to the graph, connecting it to the specified inputs.
Add a Match node to the graph.
Add a node to the graph, connecting it to the specified inputs.
Add a tag node to the graph.
Annotate an input port of the graph with a type.
Annotate an output port of the graph with a type.
Copy a value to two output ports.
Add a discard node, discarding the value at the specified output port.
Iterator over all edges in the graph.
Load from protobuf message.
Retrieve an edge from the graph by source and target ports.
Iterator over incoming edges to a node.
Inline boxes by inserting the graphs they contain in to the parent graph.
A list of all output ports of the input node (the inputs to the graph).
Given a graph and wires to connect to that graph's inputs, inline that graph into <self>, and return the outputs of the inserted graph.
Utility function to create a pair from two values.
Utility function to create a vector from an iterator of incoming wires.
Iterator over all nodes in the graph.
If there is an edge at port, return it, else None.
Iterator over outgoing edges from a node.
A list of all input ports of the output node (the outputs of the graph).
Remove an edge from the graph.
Remove nodes from the graph by index.
Set outputs of the graph by keyword arguments mapping output port names to incoming wires.
Build protobuf message from graph.
to_python
Utility function to unpack a pair into two values.
Utility to pop n_elements from a vector and return the corresponding output ports.
Attributes
Get a reference to the input node of the graph.
input_node_idx
The number of nodes in the graph.
Get a reference to the output node of the graph.
output_node_idx
- exception MissingEdge(source: tierkreis.core.tierkreis_graph.NodePort, target: tierkreis.core.tierkreis_graph.NodePort)[source]#
Bases:
Exception
- with_traceback()#
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- add_box(graph: TierkreisGraph, /, **kwargs: NodePort | NodeRef) NodeRef [source]#
Add a box node by specifying the inner graph and the inputs to connect to the node.
- add_const(value: Any) NodeRef [source]#
Add a constant value to the graph. Non-tierkreis values are converted if possible.
- add_edge(source: NodePort | NodeRef, node_port_to: NodePort, edge_type: Type | TierkreisType | None = None) TierkreisEdge [source]#
Add an edge to the graph, connecting output port to input port.
- Parameters:
source – Source of the edge, either an output port or a node with a single output.
node_port_to – Target input port.
edge_type – Optional type annotation, defaults to None
- Raises:
MismatchedGraphs – If source and target nodes are not in the same graph.
ValueError – If there is already an edge from source to target.
- Returns:
Reference to the added edge.
- Return type:
- add_func(_tk_function: str | FunctionName, _retry_secs: int | None = None, /, **kwargs: NodePort | NodeRef) NodeRef [source]#
Add a function call to the graph, connecting it to the specified inputs.
- Parameters:
_tk_function – Name of the function to call.
_retry_secs – Optionally specify how long to wait before retrying, defaults to None
- Returns:
Reference to the added node.
- Return type:
- add_match(variant_value: NodePort | NodeRef, /, **variant_handlers: NodePort | NodeRef) NodeRef [source]#
Add a Match node to the graph. The variant value is the only positional argument, handlers are specified as keyword arguments.
- add_node(_tk_node: TierkreisNode, /, **incoming_wires: NodePort | NodeRef) NodeRef [source]#
Add a node to the graph, connecting it to the specified inputs. The node to add is the only positional argument, the remaining keyword arguments specify the input port and the sources to connect to those ports.
- Parameters:
_tk_node – The node to add to the graph.
- Returns:
A reference to the added node.
- Return type:
- add_tag(tag: str, *, value: NodePort | NodeRef) NodeRef [source]#
Add a tag node to the graph. Tag the value with the specified tag string.
- annotate_input(input_port: str, edge_type: Type | TierkreisType | None)[source]#
Annotate an input port of the graph with a type.
- annotate_output(output_port: str, edge_type: Type | TierkreisType | None)[source]#
Annotate an output port of the graph with a type.
- copy_value(value: NodePort | NodeRef) Tuple[NodePort, NodePort] [source]#
Copy a value to two output ports.
- discard(out_port: NodePort) None [source]#
Add a discard node, discarding the value at the specified output port.
- edges() Iterator[TierkreisEdge] [source]#
Iterator over all edges in the graph.
- classmethod from_proto(pg_graph: Graph) TierkreisGraph [source]#
Load from protobuf message.
- get_edge(source: NodePort, target: NodePort) TierkreisEdge [source]#
Retrieve an edge from the graph by source and target ports. Raises MissingEdge if the edge does not exist.
- in_edges(node: NodeRef | int) Iterator[TierkreisEdge] [source]#
Iterator over incoming edges to a node.
- inline_boxes(recursive=False) TierkreisGraph [source]#
Inline boxes by inserting the graphs they contain in to the parent graph. Optionally do this recursively.
- Returns:
Inlined graph
- Return type:
- inputs() List[str] [source]#
A list of all output ports of the input node (the inputs to the graph).
- insert_graph(graph: TierkreisGraph, /, **kwargs: NodePort | NodeRef) Dict[str, NodePort | NodeRef] [source]#
Given a graph and wires to connect to that graph’s inputs, inline that graph into <self>, and return the outputs of the inserted graph.
- make_pair(first_port: NodePort | NodeRef, second_port: NodePort | NodeRef) NodePort [source]#
Utility function to create a pair from two values.
- make_vec(element_ports: Iterable[NodePort | NodeRef]) NodePort [source]#
Utility function to create a vector from an iterator of incoming wires.
- nodes() Iterator[TierkreisNode] [source]#
Iterator over all nodes in the graph.
- out_edge_from_port(source: NodePort) TierkreisEdge | None [source]#
If there is an edge at port, return it, else None.
- out_edges(node: NodeRef | int) Iterator[TierkreisEdge] [source]#
Iterator over outgoing edges from a node.
- outputs() List[str] [source]#
A list of all input ports of the output node (the outputs of the graph).
- remove_edge(edge: TierkreisEdge)[source]#
Remove an edge from the graph.
- set_outputs(**kwargs: NodePort | NodeRef) None [source]#
Set outputs of the graph by keyword arguments mapping output port names to incoming wires.