Parse Python file and evaluate selected functions

核能气质少年 提交于 2020-01-04 06:14:32

问题


I have a file that contains several python functions, each with some statements.

def func1():
    codeX...
def func2():
    codeY...

codeX and codeY can be multiple statements. I want to be able to parse the file, find a function by name, then evaluate the code in that function.

With the ast module, I can parse the file, find the FunctionDef objects, and get the list of Stmt objects, but how do I turn this into bytecode that I can pass to eval? Should I use the compile module, or the parser module instead?

Basically, the function defs are just used to create separate blocks of code. I want to be able to grab any block of code given the name and then execute that code in eval (providing my own local/global scope objects). If there is a better way to do this than what I described that would be helpful too.

Thanks


回答1:


Using Python 2.6.4:

text = """
def fun1():
    print 'fun1'

def fun2():
    print 'fun2'

"""

import ast
tree = ast.parse(text)
# tree.body[0] contains FunctionDef for fun1, tree.body[1] for fun2

wrapped = ast.Interactive(body=[a.body[1]])
code = compile(wrapped, 'yourfile', 'single')
eval(code)
fun2() # prints 'fun2'

Take a look at grammar in ast doc: http://docs.python.org/library/ast.html#abstract-grammar. Top-level statement must be either Module, Interactive or Expression, so you need to wrap function def in one of those.




回答2:


I want to be able to grab any block of code given the name and then execute that code ... (providing my own local/global scope objects).

A naive solution looks like this. This is based on the assumption that the functions don't all depend on global variables.

from  file_that_contains_several_python_functions import *
Direction = some_value
func1()
func2()
func3()

That should do exactly what you want.

However, if all of your functions rely on global variables -- a design that calls to mind 1970's-era FORTRAN -- then you have to do something slightly more complex.

 from  file_that_contains_several_python_functions import *
 Direction = some_value
 func1( globals() )
 func2( globals() )
 func3( globals() )

And you have to rewrite all of your global-using functions like this.

 def func1( context )
     globals().update( context )
     # Now you have access to all kinds of global variables

This seems ugly because it is. Functions which rely entirely on global variables are not really the best idea.




回答3:


If you're using Python 2.6 or later, then the compile() function accepts AST objects in addition to source code.

>>> import ast
>>> a = ast.parse("print('hello world')")
>>> x = compile(a, "(none)", "exec")
>>> eval(x)
hello world

These modules have all been rearranged for Python 3.



来源:https://stackoverflow.com/questions/4650110/parse-python-file-and-evaluate-selected-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!