Dependency Algorithm
Here is my take on an algorithm in Python that resolves dependencies. The best way to illustrate how this works is with an example...
Example
Let's say that we have the following dictionary where the keys are items, and the values are the dependencies of those items. Each dependency must itself be an item, and items with no dependencies have an empty list [] as they dependency:
my_items = {
'A': ['B', 'C', 'D'], # -- A is dependent on B, C, D,
'B': [], # -- B is dependent on nothing, etc.
'C': ['D'],
'D': ['B', 'E'],
'E': ['F'],
'F': [],
'Z': ['A', 'B', 'C', 'D']
}
Note that we've only provided a partial dictionary of items to their dependencies...for example, notice how C is dependent on D, which is dependent on B (no dependencies) and E, which is dependent on F...therefore, C is dependent on D, B, E, and F.
We can use the Dependencies class to get the complete list of dependencies for an item, like so:
from dependency_algorithm import Dependencies
# Creating a Dependencies object
dependencies = Dependencies(my_items)
dependencies.complete_dependencies("C")
>>> ['D', 'B', 'E', 'F']
More importantly, we can return the items in an order such that the dependencies resolve:
dependencies.resolve_dependencies()
>>> ['B', 'F', 'E', 'D', 'C', 'A', 'Z']
In many cases, there are multiple correct ordering of our items such that each item's dependencies resolve. If we're interested in all possible correct orderings, the Dependencies class can permutate over all possible orderings, and identify the correct ones (albeit at a high computational cost), like so:
dependencies.all_possible_resolution_orders(verbose=True)
>>> Number of permutations: 5040
>>> Number of correct orderings: 3
>>> Number of incorrect orderings: 5037
>>> [('B', 'F', 'E', 'D', 'C', 'A', 'Z'),
>>> ('F', 'B', 'E', 'D', 'C', 'A', 'Z'),
>>> ('F', 'E', 'B', 'D', 'C', 'A', 'Z')]
That's pretty much it! The Dependencies class also performs two checks, one for any dependencies that are "missing" (i.e., they are not keys in the input dictionary of items and dependencies), and another for cirular dependencies (i.e., A is dependent on B which is dependent on A which is...and so on...).
Installation
pip install dependency_algorithm
Running the unit tests with pytest
git clone https://github.com/jakesherman/dependency_algorithm.git
cd dependency_algorithm
pip install -e .
python -m pytest
Future work
- New version of
Dependencies._enhanced_list_dependenciesthat uses iteration instead of recursion - Improved version of
Dependencies.all_possible_resolution_ordersthat uses a more efficient algorithm than looping through permutations, ex. a recursive algorithm
Metadata
Release files for dependency-algorithm 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dependency_algorithm-0.1.tar.gz | 7.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dependency_algorithm-0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.1 kB
Release files / dependency_algorithm-0.1.tar.gz
| Download URL | dependency_algorithm-0.1.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
99367ae271f62fb49affb5d1b3379581419f2811c25b94b4d1301c69ae3756d7
|
|
BLAKE2b-256 checksum How to use checksums |
b3ea9789e8fbafa91bc0cd03d1b790495c8adfa4d11e91a46dedb0ea1d64e640
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.1
|
Release files / dependency_algorithm-0.1-py3-none-any.whl
| Download URL | dependency_algorithm-0.1-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
50006646054ea6a2df39a0b65f5515a0e88be56475cf5d35fe95e943f9306205
|
|
BLAKE2b-256 checksum How to use checksums |
74b6fb38cfc57ea4fbdc3174374c9157513d92c12566e4883b9c1d963e3e091f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.1
|