Reputation: 1488
I feel this isn't a very good question, but here we go:
Does a library exist that can look at a python file (as text) and disassemble it into 'bits', say into a dictionary/array. I could then step through the array and pick out classes, functions, variables etc.
I'm looking to build a tool that can analyses an entire project and lists the classes/modules in relation to each other.
The only way I can think of doing it alone would be stepping through each line and doing a lot of regex on it.
Upvotes: 0
Views: 92
Reputation: 1122042
You are looking for the ast
module, which lets you analyse and traverse the abstract syntax tree of python code.
The compile()
function lets you compile a python source file into a AST, but the module itself provides a helper function too, ast.parse()
:
import ast
with open(sourcefilename, 'r') as source:
tree = ast.parse(source.read(), sourcefilename)
Someone wrote an e-book on Python AST wrangling: Green Tree Snakes - the missing Python AST docs that you might find interesting.
Upvotes: 4