Skip to content

Instantly share code, notes, and snippets.

@gubser gubser/
Last active Jun 22, 2019

What would you like to do?
Walk the abstract syntax tree to find the data dependencies of a python script
Given the python code:
out.p = inp.x + inp.y
out.q = inp.y + 1
It finds these dependencies:
inputs are x, y
outputs are p, q
import ast
import json
import sys
class DependencyFinder(ast.NodeVisitor):
inputs = set()
outputs = set()
def visit_Attribute(self, node):
if type(node.value) is ast.Name:
if == 'inp':
elif == 'out':
if len(sys.argv) > 1:
finder = DependencyFinder()
with open(sys.argv[1], 'r', encoding='utf-8') as f:
code =
tree = ast.parse(code)
print("inputs are", ', '.join(sorted(finder.inputs)))
print("outputs are", ', '.join(sorted(finder.outputs)))
print(f"usage: {sys.argv[0]} <python-file>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.