Skip to content Skip to sidebar Skip to footer

Generate Ast From Constituent Elements Without Using Ast.parse In Python

Using the python ast module, it is possible to generate a simple abstract syntax tree as follows: import ast module = ast.parse('x=3') This generates a Module object for which the

Solution 1:

I think I just found it. using ast.dump one can inspect the contents of the tree as follows:

import astor, astmodule= ast.parse('x=3')
ast.dump(module)

This results in the following output which reveals the underlying structure:

"Module(body=[Assign(targets=[Name(id='x', ctx=Store())], value=Num(n=3))])"

We can make use of this information to build the same tree from scratch, and then use astor to recover the source:

module = ast.Module(body=[ast.Assign(targets=[ast.Name(id='x', ctx=ast.Store())], value=ast.Num(n=3))])
astor.to_source(module)

Which outputs the following:

'x = 3\n'

There is one problem however, since executing this new tree results in an error:

exec(compile(module, filename='<ast>', mode="exec"))

Traceback (most recent call last): File "", line 1, in TypeError: required field "lineno" missing from stmt

To fix this, line numbers must be added to each node using the ast.fix_missing_locations method.

Post a Comment for "Generate Ast From Constituent Elements Without Using Ast.parse In Python"