From 04bd8edd69ff6ee07df7abdaaef190e4d0c3b5bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=87=95=E9=B9=8F?= Date: Tue, 20 Jul 2021 11:58:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=8F=82=E6=95=B0=E4=BC=A0?= =?UTF-8?q?=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 22 ++++++++++++ src/main/java/JavaRunPythonFunc.java | 1 + src/main/java/JavaRunPythonFuncMapParms.java | 37 ++++++++++++++++++++ src/main/java/Vo.java | 19 ++++++++++ src/main/java/javaPythonFuncFileParams.py | 11 ++++++ 5 files changed, 90 insertions(+) create mode 100644 src/main/java/JavaRunPythonFuncMapParms.java create mode 100644 src/main/java/Vo.java create mode 100644 src/main/java/javaPythonFuncFileParams.py diff --git a/pom.xml b/pom.xml index 00cc722..b47b23d 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,12 @@ jython-standalone 2.7.0 + + org.projectlombok + lombok + true + 1.18.20 + @@ -26,4 +32,20 @@ + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + \ No newline at end of file diff --git a/src/main/java/JavaRunPythonFunc.java b/src/main/java/JavaRunPythonFunc.java index c0a8906..c5c7f3d 100644 --- a/src/main/java/JavaRunPythonFunc.java +++ b/src/main/java/JavaRunPythonFunc.java @@ -1,3 +1,4 @@ +import org.python.core.PyDictionary; import org.python.core.PyFunction; import org.python.core.PyInteger; import org.python.core.PyObject; diff --git a/src/main/java/JavaRunPythonFuncMapParms.java b/src/main/java/JavaRunPythonFuncMapParms.java new file mode 100644 index 0000000..cfa53b3 --- /dev/null +++ b/src/main/java/JavaRunPythonFuncMapParms.java @@ -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 map = new HashMap(); + 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 voList = (List) pyobj.__tojava__(List.class); + System.out.println("the list anwser is: " + voList); + } +} diff --git a/src/main/java/Vo.java b/src/main/java/Vo.java new file mode 100644 index 0000000..bc8693a --- /dev/null +++ b/src/main/java/Vo.java @@ -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; +} diff --git a/src/main/java/javaPythonFuncFileParams.py b/src/main/java/javaPythonFuncFileParams.py new file mode 100644 index 0000000..f0bdeb8 --- /dev/null +++ b/src/main/java/javaPythonFuncFileParams.py @@ -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 \ No newline at end of file