Reputation:
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
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