Skip to main content

Automatically identify and order dependencies so that they resolve correctly

Project description

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_dependencies that uses iteration instead of recursion
  • Improved version of Dependencies.all_possible_resolution_orders that uses a more efficient algorithm than looping through permutations, ex. a recursive algorithm

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

dependency_algorithm-0.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

dependency_algorithm-0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file dependency_algorithm-0.1.tar.gz.

File metadata

  • Download URL: dependency_algorithm-0.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for dependency_algorithm-0.1.tar.gz
Algorithm Hash digest
SHA256 99367ae271f62fb49affb5d1b3379581419f2811c25b94b4d1301c69ae3756d7
MD5 fbcd6cf4a73f9255524c54fb62a8bae9
BLAKE2b-256 b3ea9789e8fbafa91bc0cd03d1b790495c8adfa4d11e91a46dedb0ea1d64e640

See more details on using hashes here.

File details

Details for the file dependency_algorithm-0.1-py3-none-any.whl.

File metadata

  • Download URL: dependency_algorithm-0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for dependency_algorithm-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50006646054ea6a2df39a0b65f5515a0e88be56475cf5d35fe95e943f9306205
MD5 68389653b2fc5986a327cbe0bb2aba29
BLAKE2b-256 74b6fb38cfc57ea4fbdc3174374c9157513d92c12566e4883b9c1d963e3e091f

See more details on using hashes here.

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