You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Got a recursive algo working
* More progress
* Working algo?
* Working algo :)
* Working on ordering creator
* Structuring as package, creating the final class
* Getting close to being ready for release :)
* Unit tests passed
* Better README
* More README
* Moving notebooks into the scratch folder
* Removing old tests
* Making it clear that this is a Python 3.7 package
Here is my take on an algorithm in Python that resolves dependencies. I walk through the process of creating the algorithm in `dependency_algo.ipynb`, and implement it in a `Dependencies` class in `Dependencies.py`. The `Dependencies` class accepts as an input a dictionary of items as keys and lists of dependencies as values (see below). Alternatively, you may use the `add_item` method to add items to a `Dependencies` object.
4
-
5
-
I did not look at other dependency algorithms when creating this, but afterwards I found some [great work by Ferry Boender](http://www.electricmonk.nl/log/2008/08/07/dependency-resolving-algorithm/) that used recursion to resolve dependencies. My solutions do not use recursion.
3
+
Here is my take on an algorithm in Python 3.7 that resolves dependencies. The best way to illustrate how this works is with an example...
6
4
7
5
## Example
8
6
9
-
Dictionary of items we want to resolve dependencies for:
7
+
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:
10
8
11
9
```python
12
10
my_items = {
@@ -20,42 +18,67 @@ my_items = {
20
18
}
21
19
```
22
20
23
-
We can use the `Dependencies` class to take these items and order them.
21
+
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.
22
+
23
+
We can use the `Dependencies` class to get the complete list of dependencies for an item, like so:
24
24
25
25
```python
26
-
import collections
27
-
from Dependencies import Dependencies
26
+
from dependency_algorithm import Dependencies
28
27
29
28
# Creating a Dependencies object
30
29
dependencies = Dependencies(my_items)
31
-
32
-
# The order method returns an OrderedDict
33
-
print Dependencies(my_items).order()
30
+
dependencies.complete_dependencies("C")
34
31
```
35
32
36
33
```
37
-
>>> OrderedDict([('B', []),
38
-
>>> ('F', []),
39
-
>>> ('E', ['F']),
40
-
>>> ('D', ['B', 'E']),
41
-
>>> ('C', ['D']),
42
-
>>> ('A', ['B', 'C', 'D']),
43
-
>>> ('Z', ['A', 'B', 'C', 'D'])])
34
+
>>> ['D', 'B', 'E', 'F']
44
35
```
45
36
46
-
We can also output a list of the item names in order
37
+
More importantly, we can return the items in an order such that the dependencies resolve:
47
38
48
39
```python
49
-
print Dependencies(my_items).order_list()
40
+
dependencies.resolve_dependencies()
50
41
```
51
42
52
43
```
53
44
>>> ['B', 'F', 'E', 'D', 'C', 'A', 'Z']
54
45
```
55
46
47
+
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:
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...).
-`dependency_algo.ipynb` Jupyter Notebook where I walk through the steps I used in creating the algorithm
60
-
-`Dependencies.py` contains the `Dependencies` class used to order a dictionary of items and their dependencies
61
-
-`tests.py` has a few quick tests to check to make sure that the `Dependencies` class is functioning correctly
83
+
* New version of `Dependencies._enhanced_list_dependencies` that uses iteration instead of recursion
84
+
* Improved version of `Dependencies.all_possible_resolution_orders` that uses a more efficient algorithm than looping through permutations, ex. a recursive algorithm
0 commit comments