Skip to content

pkulev/topsort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Python implementation of topological sort

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages