Skip to content

Instantly share code, notes, and snippets.

@gubser gubser/analyzer.py
Last active Jun 22, 2019

Embed
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 node.value.id == 'inp':
self.inputs.add(node.attr)
elif node.value.id == 'out':
self.outputs.add(node.attr)
self.generic_visit(node)
if len(sys.argv) > 1:
finder = DependencyFinder()
with open(sys.argv[1], 'r', encoding='utf-8') as f:
code = f.read()
tree = ast.parse(code)
finder.visit(tree)
print("inputs are", ', '.join(sorted(finder.inputs)))
print("outputs are", ', '.join(sorted(finder.outputs)))
else:
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.