Du kan inte välja fler än 25 ämnen
Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
37 rader
1.5 KiB
37 rader
1.5 KiB
import org.python.core.*;
|
|
import org.python.util.PythonInterpreter;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @author yanpeng
|
|
* @version 1.0
|
|
* @desc TODO
|
|
* @company 北京中经网软件有限公司
|
|
* @date 2021/7/16 17:43
|
|
*/
|
|
public class JavaRunPythonFuncMapParms {
|
|
public static void main(String[] args) {
|
|
PythonInterpreter interpreter = new PythonInterpreter();
|
|
interpreter.execfile("F:\\workspace\\python\\demo\\javaPythonFuncFileParams.py");
|
|
Map<PyObject, PyObject> map = new HashMap<PyObject, PyObject>();
|
|
map.put(new PyString("nelson"), PyJavaType.wrapJavaObject("yasaka"));
|
|
// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型
|
|
PyFunction pyFunction = interpreter.get("add", PyFunction.class);
|
|
//调用函数,如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”
|
|
PyObject pyobj = pyFunction.__call__(new PyDictionary(map));
|
|
System.out.println("the anwser is: " + pyobj);
|
|
|
|
Vo vo = Vo.builder().age(1).id("nelson").name("nelson name").build();
|
|
Vo vo1 = Vo.builder().age(2).id("yasaka").name("yasaka name").build();
|
|
PyList pyList = new PyList();
|
|
pyList.add(vo);
|
|
pyList.add(vo1);
|
|
|
|
pyobj = pyFunction.__call__(pyList);
|
|
List<Vo> voList = (List<Vo>) pyobj.__tojava__(List.class);
|
|
System.out.println("the list anwser is: " + voList);
|
|
}
|
|
}
|
|
|