Welcome to DotPruner’s documentation!

This module exposes the entry points for using dotpruner, either in code or as a CLI.

dotpruner.dotpruner.process_from_file(filename: str, *, node_picker=<built-in function min>) → pydot.Graph

Prune DOT graph from file.

Parameters
  • filename (str) – The file location of the original DOT graph

  • node_picker – A function that, given the names of two nodes to be pruned, picks the node to preserved. Defaults to min.

Returns

the pruned DOT graph

Return type

pydot.Graph

dotpruner.dotpruner.process_from_string(data: str, *, node_picker=<built-in function min>) → pydot.Graph

Prune DOT graph from string representation.

Parameters
  • data (str) – The original DOT graph as a string

  • node_picker – A function that, given the names of two nodes to be pruned, picks the node to preserved. Defaults to min.

Returns

the pruned DOT graph

Return type

pydot.Graph

This module holds utility methods for parsing components of DOT graphs and performing the actual pruning.

class dotpruner.utils.Node

A class to represent outgoing edges connected to a node.

add_edge(dst, attrs)

Add outgoing edge with attributes attrs to dst node.

Parameters
  • dst – Name of destination node

  • attrs – Key-value mapping of edge attributes

edges(*, transform_key=<function <lambda>>)

Gets the outgoing edges connected to this node.

Parameters

transform_key – A function to transform the destination node value. Defaults to the identity function.

Returns

A list of tuples, with each tuple defined by the (possibly transformed) endpoint and attributes of each edge. The list is sorted in ascending order of names.

dotpruner.utils.parse_edges(graph: pydot.Graph) → Mapping[str, dotpruner.utils.Node]

Parse edges from graph.

Parameters

graph (pydot.Graph) – The graph to parse

Returns

A mapping of name to edges for each node.

Return type

typing.Mapping[str, Node]

dotpruner.utils.parse_nodes(graph: pydot.Graph) → Mapping[str, Mapping[str, str]]

Parse nodes from graph.

Parameters

graph (pydot.Graph) – The graph to parse

Returns

A mapping of name to attributes for each node.

Return type

typing.Mapping[str, typing.Mapping]

dotpruner.utils.prune_graph(node_to_edges: Mapping[str, dotpruner.utils.Node], initial_lookup: Mapping[str, str], *, pick_node) → Mapping[str, str]

Prunes the graph of nodes that share the same outgoing edges.

This works by ‘merging’ nodes using a lookup table, initially initial_lookup: if two nodes x and y are the same, the pick_node function defines the node to be preserved. Suppose x is to be preserved: then the lookup table maps y to x.

This process terminates when one iteration over the node_to_edges mapping results in no nodes being pruned. The function returns the final lookup table defining the nodes that have been ‘merged’.

Parameters
  • node_to_edges (typing.Mapping[str, Node]) – A mapping of name to edges for each node.

  • initial_lookup (typing.Mapping[str, str]) – Lookup table for pruning.

  • node_picker – A function that, given the names of two nodes to be pruned, picks the node to preserved.

Returns

typing.Mapping[str, str] The final lookup table that maps nodes in the original graph to their ‘pruned version’.

dotpruner.utils.same_graph(graph_1: str, graph_2: str) → bool

Compares the string representation of two DOT graphs for equality.

Parameters
  • graph_1 (str) – String representation of graph 1

  • graph_2 (str) – String representation of graph 2

Returns

True iff graph_1 equals graph_2

Return type

bool

Indices and tables