While many useful operations may take place between parsing and bytecode generation, the simplest operation is to do nothing. For this purpose, using the parser module to produce an intermediate data structure is equivelent to the code
>>> code = compile('a + 5', 'eval')
>>> a = 5
>>> eval(code)
10
>>> import parser
>>> ast = parser.expr('a + 5')
>>> code = parser.compileast(ast)
>>> a = 5
>>> eval(code)
10
import parser
def load_suite(source_string):
ast = parser.suite(source_string)
code = parser.compileast(ast)
return ast, code
def load_expression(source_string):
ast = parser.expr(source_string)
code = parser.compileast(ast)
return ast, code