Python AST with preserved comments
An AST that keeps information about formating, comments etc. is called a Full Syntax Tree.
redbaron is able to do this. Install with pip install redbaron
and try the following code.
import redbaron
with open("/path/to/module.py", "r") as source_code:
red = redbaron.RedBaron(source_code.read())
print (red.fst())
The ast
module doesn't include comments. The tokenize
module can give you comments, but doesn't provide other program structure.