17

Suppose I have the following string:

code = """
if 1 == 1 and 2 == 2 and 3 == 3:
    test = 1
"""

The following code converts that string in a AST.

ast.parse(code)

Then I have a tree like:

Module(body=[<_ast.If object at 0x100747358>])
  If(test=BoolOp(op=And(), values=[<_ast.Compare object at 0x100747438>, <_ast.Compare object at 0x100747a90>, <_ast.Compare object at 0x100747d68>]), body=[<_ast.Assign object at 0x100747e48>], orelse=[])

I want to know if there is a way to convert the object at.If into the string if 1 == 1 and 2 == 2 and 3 == 3:

I know it can be done traversing the children node, but it's getting too complicated that way.

2
  • 2
    traversing the children node, <- that's the only way I know how to do it Commented Oct 13, 2016 at 20:06
  • There are 3rd party tools that make a credible attempt, e.g. astor Commented Sep 18, 2017 at 2:44

3 Answers 3

18

Python 3.9 introduced ast.unparse which does exactly this, i.e. it reverses ast.parse. Using your example:

import ast

code = """
if 1 == 1 and 2 == 2 and 3 == 3:
    test = 1
"""

tree = ast.parse(code)
print(ast.unparse(tree))

This will print out:

if 1 == 1 and 2 == 2 and (3 == 3):
    test = 1

Note that there may be slight differences to the original input.

Sign up to request clarification or add additional context in comments.

Comments

12

ast.get_source_segment was added in python 3.8:

>>> import ast

>>> code = """
>>> if 1 == 1 and 2 == 2 and 3 == 3:
>>>     test = 1
>>> """
>>> node = ast.parse(code)
>>> ast.get_source_segment(code, node.body[0])
'if 1 == 1 and 2 == 2 and 3 == 3:\n    test = 1'

Comments

7

You can use the astunparse library which was basically just code from the core, repackaged individually.

First, install the library:

pip install astunparse

Then, run your AST module through it to get the source right back. So running:

import ast
import astunparse

code = """
if 1 == 1 and 2 == 2 and 3 == 3:
    test = 1
"""

node = ast.parse(code)

astunparse.unparse(node)

Will output:

'\nif ((1 == 1) and (2 == 2) and (3 == 3)):\n    test = 1\n'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.