Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/python/design_ast_example1.py
1240 views
1
#!/usr/bin/python
2
#$Id$
3
4
"""
5
This program is directly executing it a Abstract Syntax Tree Level.
6
This is just doing xy*3.
7
"""
8
import ast
9
10
node = ast.Expression(ast.BinOp(
11
ast.Str('xy'),
12
ast.Mult(),
13
ast.Num(3)))
14
15
fixed = ast.fix_missing_locations(node)
16
17
codeobj = compile(fixed, '<string>', 'eval')
18
print(eval(codeobj))
19
20