Skip to main content

Rust port of the Python stdlib graphlib modules

Project description

graphlib2

CI

This is a Rust port of Python's stdlib graphlib. It passes all of the standard libraries tests and is a drop in replacement. This also happens to be Python 3.7 compatible, so it can be used as a backport. Since usage is exactly the same as the standard libraries, please refer to their documentation for usage details.

See this project on GitHub.

Example

from graphlib2 import TopologicalSorter

graph = {0: [1], 1: [2]}  # 0 depends on 1, 1 depends on 2
ts = TopologicalSorter(graph)
ts.prepare()
while ts.is_active():
    ready_nodes = ts.get_ready()
    ts.done(*ready_nodes)  # all at a time or one by one

Motivation

This was primarily written for di and for me to learn Rust. In other words: please vet the code yourself before using this.

Differences with the stdlib implementation

  1. Added TopologicalSorter.copy() which copies a prepared or unprepared graph so that it can be executed multiple times.
  2. Pretty solid performance improvements (see benchmarks).
  3. Misc improvements, like working generics without postponed evaluation (ToplologicalSorter[int] works at runtime).

Performance

The implementation was designed for the specific use case of adding all nodes, calling prepare() then copying and executing in a loop:

from graphlib2 import TopologicalSorter

graph = {0: [1], 1: [2]}
ts = TopologicalSorter(graph)
ts.prepare()
while True:  # hot loop
    t = ts.copy()
    while t.is_active():
        ready_nodes = t.get_ready()
        t.done(*ready_nodes)

This means that the focus is on the performance of TopologicalSorter.get_ready() and TopologicalSorter.done(), and only minimal effort was put into other methods (prepare(), add() and get_static_order()), although these are still quite performant.

Contributing

  1. Clone the repo.
  2. Run make init
  3. Run make test
  4. Make your changes
  5. Push and open a pull request
  6. Wait for CI to run.

If your pull request gets approved and merged, it will automatically be relased to PyPi (every commit to main is released).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphlib2-0.4.7.tar.gz (390.5 kB view hashes)

Uploaded Source

Built Distributions

graphlib2-0.4.7-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (223.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

graphlib2-0.4.7-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (207.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

graphlib2-0.4.7-pp39-pypy39_pp73-macosx_10_7_x86_64.whl (203.7 kB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

graphlib2-0.4.7-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (223.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

graphlib2-0.4.7-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (207.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

graphlib2-0.4.7-pp38-pypy38_pp73-macosx_10_7_x86_64.whl (203.8 kB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

graphlib2-0.4.7-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (225.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

graphlib2-0.4.7-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (209.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

graphlib2-0.4.7-pp37-pypy37_pp73-macosx_10_7_x86_64.whl (205.6 kB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

graphlib2-0.4.7-cp37-abi3-win_amd64.whl (150.5 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

graphlib2-0.4.7-cp37-abi3-win32.whl (145.1 kB view hashes)

Uploaded CPython 3.7+ Windows x86

graphlib2-0.4.7-cp37-abi3-musllinux_1_2_x86_64.whl (394.6 kB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ x86-64

graphlib2-0.4.7-cp37-abi3-musllinux_1_2_i686.whl (419.3 kB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ i686

graphlib2-0.4.7-cp37-abi3-musllinux_1_2_armv7l.whl (475.7 kB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARMv7l

graphlib2-0.4.7-cp37-abi3-musllinux_1_2_aarch64.whl (387.6 kB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARM64

graphlib2-0.4.7-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (223.1 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

graphlib2-0.4.7-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (278.2 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ s390x

graphlib2-0.4.7-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (311.7 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64le

graphlib2-0.4.7-cp37-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (322.4 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64

graphlib2-0.4.7-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (212.6 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARMv7l

graphlib2-0.4.7-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (206.9 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

graphlib2-0.4.7-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl (247.1 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.5+ i686

graphlib2-0.4.7-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (389.0 kB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

graphlib2-0.4.7-cp37-abi3-macosx_10_7_x86_64.whl (203.7 kB view hashes)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page