燕鹏
3 years ago
5 changed files with 90 additions and 0 deletions
@ -0,0 +1,37 @@ |
|||||
|
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); |
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
import lombok.Builder; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
/** |
||||
|
* @author yanpeng |
||||
|
* @version 1.0 |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2021/7/20 11:40 |
||||
|
*/ |
||||
|
@Data |
||||
|
@Builder |
||||
|
public class Vo implements Serializable { |
||||
|
private String id; |
||||
|
private String name; |
||||
|
private Integer age; |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
def add(a): |
||||
|
print(a) |
||||
|
print(type(a)) |
||||
|
if type(a) == list: |
||||
|
print(a[0]) |
||||
|
print(type(a[0])) |
||||
|
print(a[0].name) |
||||
|
print(type(a[0].name)) |
||||
|
print(a[0].age) |
||||
|
print(type(a[0].age)) |
||||
|
return a |
Loading…
Reference in new issue