Registry / data / graphlib-backport

graphlib-backport

JSON →
library1.1.0pypypi✓ verified 83d ago

Backport of the Python 3.9 `graphlib` module for Python 3.6+. It provides the `TopologicalSorter` class for performing topological sorting on directed acyclic graphs (DAGs). The library is currently at version 1.1.0 and is actively maintained, with releases tied to updates on its GitHub repository.

pip install graphlib-backport
INSTALL
IMPORT
SIG · GRAPHLIB-BACKPORT
G
graphlib-backport
datapythonv1.1.0
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TopologicalSorter
from graphlib import TopologicalSorter
from graphlib_backport import TopologicalSorter
CycleError
from graphlib import CycleError

This quickstart demonstrates the core functionality of `TopologicalSorter` to process a graph. It shows both obtaining a full static order and processing nodes dynamically, which is useful for parallel execution patterns. Nodes are represented as hashable objects (e.g., strings), and dependencies are defined by providing a set of predecessors for each node.

from graphlib_backport import TopologicalSorter # Example graph: keys are nodes, values are sets of predecessors graph = {"D": {"B", "C"}, "C": {"A"}, "B": {"A"}, "A": set()} ts = TopologicalSorter(graph) # Get the nodes in topological order using static_order (convenience method) order = list(ts.static_order()) print(f"Topological order (static): {order}") # Example with dynamic processing (simulating parallel work) print("\nProcessing nodes dynamically:") ts_dynamic = TopologicalSorter(graph) ts_dynamic.prepare() # Must be called before is_active, get_ready, or done processed_nodes = [] while ts_dynamic.is_active(): ready_nodes = ts_dynamic.get_ready() if ready_nodes: print(f" Ready to process: {ready_nodes}") # Simulate processing for each ready node for node in ready_nodes: processed_nodes.append(node) ts_dynamic.done(*ready_nodes) # Mark nodes as done to unblock successors else: # This branch should not be reached in a valid DAG if is_active is true # unless there's an unexpected state (e.g., cycle detected after prepare, or bug). print(" No nodes ready, but sorter is active. Potential issue or cycle?") break print(f"Dynamic processing complete. Order: {processed_nodes}")
Debug
Known issues
gotchaWhen installed on Python versions 3.9 or higher, the native `graphlib` module (which includes `TopologicalSorter`) will take precedence if you try to import directly as `import graphlib`. It is generally recommended to limit installation of `graphlib-backport` to Python versions `<3.9` and use the standard library's `graphlib` for Python `>=3.9`.
fix
For Python 3.9+, prefer `from graphlib import TopologicalSorter`. If you must use the backport on 3.9+, ensure your imports are explicitly `from graphlib_backport import TopologicalSorter`. Utilize environment markers or poetry's `python` dependency specifier to conditionally install the backport only on older Python versions.
affects: Python 3.9+
deprecatedExplicit support for Python 3.6 and 3.7 is considered 'somewhat experimental' and is slated to be dropped, as these Python versions have reached their official end-of-life status.
fix
Migrate your project to Python 3.8 or newer. For Python 3.9+, use the standard library's `graphlib` module.
affects: Python 3.6, 3.7
gotchaThe PyPI package name for installation is `graphlib-backport` (with a hyphen), but the Python import name within your code is `graphlib_backport` (with an underscore). Attempting `import graphlib-backport` will result in a `ModuleNotFoundError`.
fix
Use `pip install graphlib-backport` for installation and `from graphlib_backport import TopologicalSorter` (or `import graphlib_backport`) for importing in your Python code.
affects: All versions
Upgrade
Version history
1.1.0latest on PyPI · released Mar 5, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
graphlib-backport — pip install graphlib-backport · libregistry