SHARE
TWEET

Untitled

a guest Feb 18th, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/python3
  2.  
  3. from typing import List, Mapping
  4.  
  5. class Node:
  6.     name: str
  7.     deps: List[str] = []
  8.  
  9.     def __init__(self, name: str, deps: List[str] = []):
  10.         self.name = name
  11.         self.deps = deps
  12.  
  13.  
  14. class Nodes:
  15.     nodes: Mapping[str, Node] = []
  16.  
  17.     def __init__(self, nodes: List[str]):
  18.         self.nodes = nodes
  19.  
  20. data = Nodes(nodes={
  21.     'a': Node(name='a', deps=['b']),
  22.     'b': Node(name='b', deps=['a']),
  23. })
  24.  
  25. def dep_resolve(name: str, resolved: List[str], unresolved: List[str) -> None:
  26.     unresolved.append(name)
  27.     node = data.nodes[name]
  28.     for edge in node.deps:
  29.         print(edge)
  30.         if edge not in resolved:
  31.             if edge in unresolved:
  32.                 raise Exception('Circular reference detected: %s -> %s' % (name, edge))
  33.             dep_resolve(edge, resolved, unresolved)
  34.     resolved.append(name)
  35.     unresolved.remove(name)
  36.  
  37.  
  38. resolved = []
  39. dep_resolve('b', resolved, [])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top