在 Java 中使用 Jython 获取 Python 个参数
Get Python arguments using Jython in Java
我正在尝试解析 python 脚本以获取函数名称及其参数,我需要使用 java.
来完成
我设法使用 Jython 获取了他们的名字,但我找不到任何方法来获取他们的参数(名称、数量?)。
Python 示例:
def multiply_by_two(number):
"""Returns the given number multiplied by two
The result is always a floating point number.
This keyword fails if the given `number` cannot be converted to number.
"""
return float(number) * 2
def numbers_should_be_equal(first, second):
print '*DEBUG* Got arguments %s and %s' % (first, second)
if float(first) != float(second):
Java代码:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(file.getAbsolutePath());
PyStringMap map=(PyStringMap)interpreter.getLocals();
for (Object key : map.keys()) {
Object o=map.get(Py.java2py(key));
if (o instanceof PyFunction) {
System.out.println((String)key); // the function name
PyFunction function = (PyFunction) o;
}
}
我开始对找到一种方法失去希望...
如果有人有想法,即使不使用 Jython
谢谢
函数的内部__code__
字典属性有一个co_varnames
函数变量名的元组属性,例如:
def t(t1, t2): pass
t.__code__.co_nlocals
>>> ('t1', 't2')
对于具有默认值的关键字参数,还有 t.__defaults__
。
由于 __code__
是内部实现,它可能会因解释器而异。据我所知,它是 Python 2.6+.
规范的一部分
我用了 from mata:
Would doing in in python using inspect.getargspec() be an option, e.g.
after execfile do something like
interpreter.exec("import inspect"); PyStringMap map=(PyStringMap)interpreter.eval("dict([(k, inspect.getargspec(v))
for (k, v) in locals().items() if inspect.isfunction(v)]) ")
这是工作代码:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(file.getAbsolutePath());
interpreter.exec("import inspect");
PyDictionary dico=(PyDictionary) interpreter.eval("dict([(k, inspect.getargspec(v).args) for (k, v) in locals().items() if inspect.isfunction(v)])");
ConcurrentMap<PyObject, PyObject> map= dico.getMap();
map.forEach((k,v)->{
System.out.println(k.toString());
System.out.println(v.toString());
});
我正在尝试解析 python 脚本以获取函数名称及其参数,我需要使用 java.
来完成我设法使用 Jython 获取了他们的名字,但我找不到任何方法来获取他们的参数(名称、数量?)。
Python 示例:
def multiply_by_two(number):
"""Returns the given number multiplied by two
The result is always a floating point number.
This keyword fails if the given `number` cannot be converted to number.
"""
return float(number) * 2
def numbers_should_be_equal(first, second):
print '*DEBUG* Got arguments %s and %s' % (first, second)
if float(first) != float(second):
Java代码:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(file.getAbsolutePath());
PyStringMap map=(PyStringMap)interpreter.getLocals();
for (Object key : map.keys()) {
Object o=map.get(Py.java2py(key));
if (o instanceof PyFunction) {
System.out.println((String)key); // the function name
PyFunction function = (PyFunction) o;
}
}
我开始对找到一种方法失去希望...
如果有人有想法,即使不使用 Jython
谢谢
函数的内部__code__
字典属性有一个co_varnames
函数变量名的元组属性,例如:
def t(t1, t2): pass
t.__code__.co_nlocals
>>> ('t1', 't2')
对于具有默认值的关键字参数,还有 t.__defaults__
。
由于 __code__
是内部实现,它可能会因解释器而异。据我所知,它是 Python 2.6+.
我用了
Would doing in in python using inspect.getargspec() be an option, e.g. after execfile do something like
interpreter.exec("import inspect"); PyStringMap map=(PyStringMap)interpreter.eval("dict([(k, inspect.getargspec(v)) for (k, v) in locals().items() if inspect.isfunction(v)]) ")
这是工作代码:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(file.getAbsolutePath());
interpreter.exec("import inspect");
PyDictionary dico=(PyDictionary) interpreter.eval("dict([(k, inspect.getargspec(v).args) for (k, v) in locals().items() if inspect.isfunction(v)])");
ConcurrentMap<PyObject, PyObject> map= dico.getMap();
map.forEach((k,v)->{
System.out.println(k.toString());
System.out.println(v.toString());
});