在Java中使用Jython获取Python参数

在Java中使用Jython获取Python参数,java,python,jython,Java,Python,Jython,我试图解析一个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 fai

我试图解析一个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


谢谢你

函数的内部
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
字典属性具有函数变量名的
co\u varnames
元组属性,例如:

def t(t1, t2): pass
t.__code__.co_nlocals

>>>  ('t1', 't2')
对于具有默认值的关键字参数,还有
t.\uuuuu默认值\uuuu

由于
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。好的,从Python 2.6+开始,它是规范的一部分。

我使用了来自:


在python中使用inspect.getargspec()是否是一个选项,例如。 在execfile之后执行以下操作

以下是工作代码:

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());   
});
PythonInterpreter解释器=新的PythonInterpreter();
explorer.execfile(file.getAbsolutePath());
翻译执行(“进口检查”);
PyDictionary dico=(PyDictionary)解释器.eval(“dict([(k,inspect.getargspec(v).args)for(k,v)in locals().items(),if inspect.isfunction(v)]”;
ConcurrentMap=dico.getMap();
图.forEach((k,v)->{
System.out.println(k.toString());
System.out.println(v.toString());
});

您好,谢谢您的回答。我正在尝试实现它,但Jython中似乎没有PyFunction的代码属性,另一个属性func_代码确实存在:PyFunction function=(PyFunction)o;PyCode代码=function.func_代码;但是它触发了这个错误:java.lang.NoSuchFieldError:func_code实际上,根据API文档,Jython中没有
\uuuuuuuuuu
<代码>\uuu获取新参数\uuuu
可能吗?-不幸的是,它也不起作用。事实上,代码apear在本文档中,而不是在本文档中,它与我所拥有的(我使用的是2.7独立jar),你知道为什么这两个文档中的文件存在这种差异吗?上一个是关于
2.5
API规范的,这解释了
\ucode\uuu
缺失的原因。看见打开的一个用于
2.7
,它应该与2.7 jython jar匹配-
\uuuuuu code\uuuuu
应该在那里,换句话说。我设法从PyFunction中获取了code属性,但是没有co\u varname或co\u nlocals…在python中使用是一个选项吗,例如,在execfile之后执行类似
解释器.exec的操作(“导入检查”);PyStringMap=(PyStringMap)解释器.eval(“局部变量中(k,v)的dict([(k,inspect.getargspec(v)))()。如果inspect.isfunction(v)],则为items()))
Perfect!我肯定没有正确的思维方式在java中使用jython。我会在你的答案中添加一个答案来结束这个主题。但是你也可以在答案中添加你的评论,以获得接受的答案!谢谢你的帮助
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());   
});