On this page
graphlib.TopologicalSorter
Prev: graphlib.CycleError | Up: graphlib
Rel: graphlib.CycleError
Ref: docs.python.org — graphlib.TopologicalSorter
Tags: #public #python3 #stdlib #graphlib
Import
import graphlib
from graphlib import TopologicalSorter
Signature & Overview
graphlib.TopologicalSorter(graph=None)
Provides functionality to topologically sort a graph of hashable nodes
Methods & Attributes
TopologicalSorter.add(self, node, *predecessors)— Add a new node and its predecessors to the graph.TopologicalSorter.done(self, *nodes)— Marks a set of nodes returned by "get_ready" as processed.TopologicalSorter.get_ready(self)— Return a tuple of all the nodes that are ready.TopologicalSorter.is_active(self)— ReturnTrueif more progress can be made andFalseotherwise.TopologicalSorter.prepare(self)— Mark the graph as finished and check for cycles in the graph.TopologicalSorter.static_order(self)— Returns an iterable of nodes in a topological order.
Usage
import graphlib
deps = {"deploy": {"build", "test"}, "test": {"build"}, "build": set()}
order = list(graphlib.TopologicalSorter(deps).static_order())
assert order.index("build") < order.index("test") < order.index("deploy")
Gotchas
- Edge direction is
node -> predecessors:{"B": {"A"}}meansAmust come beforeB. - Use
prepare()/get_ready()/done()to execute independent nodes in parallel.
REPL Inspect
from graphlib import TopologicalSorter
help(TopologicalSorter)
Linked from
Related notes
- tuts/python3/stdlib_module/graphlib/CycleError Linked here; links to this page; shared tags: #graphlib, #stdlib
- tuts/python3/stdlib_module/graphlib Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib
- tuts/c/stdlib/ctype/isdigit shared tags: #stdlib
- tuts/c/stdlib/ctype/isspace shared tags: #stdlib