Skip to content

Latest commit

 

History

History
110 lines (82 loc) · 2.54 KB

README.rst

File metadata and controls

110 lines (82 loc) · 2.54 KB

PyPI Build Status codecov.io

topsort

Python implementation of topological sort.

Requirements

This package has no dependencies.

Installation

Unsurprisingly, name topsort have been in use on PyPI for ages.

$ pip install pytopsort

Examples

from pytopsort import topsort

# This simple recipe is a complete mess, until we apply topological sort to it!
recipe = [{
    # This action depends on another
    "action": "spread peanut butter on bread",
    "after": ["slice bread"],
}, {
    # This action has no dependencies
    "action": "slice bread",
    "after": [],
}, {
    "action": "eat the delicious breakfast!",
    "after": ["spread jam on top", "make a cup of coffee"],
}, {
    "action": "make a cup of coffee",
    "after": [],
}, {
    "action": "spread jam on top",
    "after": ["spread peanut butter on bread"],
}]

recipe = topsort(
    recipe,
    deptest=lambda self, other: self["action"] in other["after"],
)

# If we then print list(recipe), it will be like:
[{
    'action': 'make a cup of coffee',
    'after': []
}, {
    'action': 'slice bread',
    'after': []
}, {
    'action': 'spread peanut butter on bread',
    'after': ['slice bread']
}, {
    'action': 'spread jam on top',
    'after': ['spread peanut butter on bread']
}, {
    'action': 'eat the delicious breakfast!',
    'after': ['spread jam on top', 'make a cup of coffee']
}]

Now we can put this recipe into the breakfast machine!

Development

Installation

$ poetry install

Testing

$ poetry run pytest -s -v tests/  # run all tests
$ poetry run pytest --cov=pytopsort -s -v tests/  # run all tests with coverage
$ poetry run black pytopsort/ tests/  # autoformat code
$ # run type checking
$ poetry run pytest --mypy --mypy-ignore-missing-imports -s -v pytopsort/ tests/
$ # run code linting
$ poetry run pylint pytopsort/

Documentation

  • To be added