user3310334
user3310334

Reputation:

Tokenise or parse Python code in Python itself

The code standard library seems to include functions for compiling and interpreting Python to some level, but I want to:

How can I do this within Python itself?

Upvotes: 1

Views: 54

Answers (1)

Caridorc
Caridorc

Reputation: 6661

You can use the standard library function ast or the more pretty astor library, here is a simple example:

import ast
import astor

code = '''
def add(a, b):
    if True:
        return a + b
    else:
        raise Exception
'''

tree = ast.parse(code)
print(astor.dump_tree(tree))

The output is a human readable representation of the Abstract Syntax Tree:

Module(
    body=[
        FunctionDef(name='add',
            args=arguments(posonlyargs=[],
                args=[arg(arg='a', annotation=None, type_comment=None),
                    arg(arg='b', annotation=None, type_comment=None)],
                vararg=None,
                kwonlyargs=[],
                kw_defaults=[],
                kwarg=None,
                defaults=[]),
            body=[
                If(test=Constant(value=True, kind=None),
                    body=[Return(value=BinOp(left=Name(id='a'), op=Add, right=Name(id='b')))],
                    orelse=[Raise(exc=Name(id='Exception'), cause=None)])],
            decorator_list=[],
            returns=None,
            type_comment=None)],
    type_ignores=[])

Upvotes: 1

Related Questions