Skip to content

Instantly share code, notes, and snippets.

@jargnar
Created March 8, 2016 16:53
Show Gist options
  • Save jargnar/0946ab1d985e2b4ab776 to your computer and use it in GitHub Desktop.
Save jargnar/0946ab1d985e2b4ab776 to your computer and use it in GitHub Desktop.
Lists all function calls in a python file
'''
Get all function calls from a python file
The MIT License (MIT)
Copyright (c) 2016 Suhas S G <jargnar@gmail.com>
'''
import ast
from collections import deque
class FuncCallVisitor(ast.NodeVisitor):
def __init__(self):
self._name = deque()
@property
def name(self):
return '.'.join(self._name)
@name.deleter
def name(self):
self._name.clear()
def visit_Name(self, node):
self._name.appendleft(node.id)
def visit_Attribute(self, node):
try:
self._name.appendleft(node.attr)
self._name.appendleft(node.value.id)
except AttributeError:
self.generic_visit(node)
def get_func_calls(tree):
func_calls = []
for node in ast.walk(tree):
if isinstance(node, ast.Call):
callvisitor = FuncCallVisitor()
callvisitor.visit(node.func)
func_calls.append(callvisitor.name)
return func_calls
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', help='Input .py file', required=True)
args = parser.parse_args()
tree = ast.parse(open(args.input).read())
print get_func_calls(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment