Created
March 10, 2017 22:12
-
-
Save SegFaultAX/e1a33011fbd1fc52cee5265e3a01c0df to your computer and use it in GitHub Desktop.
Simple Dependency graph / Transitive closure [python]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dependencies(partials): | |
shallow_dep_list = { m.name: m.inherits for m in partials } | |
def transitive1(otl): | |
deps = [] | |
rest = list(shallow_dep_list[otl]) | |
while rest: | |
e = rest.pop() | |
deps.append(e) | |
rest += [d for d in shallow_dep_list[e] if d not in deps] | |
return deps | |
return { k: transitive1(k) for k in shallow_dep_list } | |
import pprint | |
from collections import namedtuple | |
Node = namedtuple("Node", ["name", "inherits"]) | |
pprint.pprint(dependencies([ | |
Node("foo", []), # No dependency | |
Node("bar", ["foo"]), # <-- Direct | |
Node("baz", ["bar"]), # <-- Transitive | |
Node("spam", ["eggs"]), # <-- Cycle! | |
Node("eggs", ["spam"]), # <-- Cycle! | |
])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment