Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top