I am working on a parsed AST string of Python code. Now, I am at a stage where I want to convert small tree structures back to code for some analysis.
import ast
ast_string = ast.dump(ast.parse("[1,2,3]"))
print(ast_string)
# 'Module(body=[Expr(value=List(elts=[Num(n=1), Num(n=2), Num(n=3)], ctx=Load()))])'
Now, I want to convert this Module(body=[Expr(value=List(elts=[Num(n=1), Num(n=2), Num(n=3)], ctx=Load()))])
back to code.
Example:
def ast_to_code(ast_string):
....
code = ast_to_code(ast_string)
print(code) # [1,2,3]
Thanks in advance. I have searched over the web found different libraries that take a parse tree object and then convert it back to code. I wasn't able to find something that can convert any AST tree in string back to code.