Pārlūkot izejas kodu

复杂参数传递

master
燕鹏 pirms 3 gadiem
vecāks
revīzija
04bd8edd69
  1. 22
      pom.xml
  2. 1
      src/main/java/JavaRunPythonFunc.java
  3. 37
      src/main/java/JavaRunPythonFuncMapParms.java
  4. 19
      src/main/java/Vo.java
  5. 11
      src/main/java/javaPythonFuncFileParams.py

22
pom.xml

@ -13,6 +13,12 @@
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.20</version>
</dependency>
</dependencies>
<repositories>
<repository>
@ -26,4 +32,20 @@
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

1
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;

37
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<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);
}
}

19
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;
}

11
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
Notiek ielāde…
Atcelt
Saglabāt