Java與Python之間使用jython工具類實(shí)現(xiàn)數(shù)據(jù)交互
最近有個(gè)功能需要java與python之間的數(shù)據(jù)交互,java需要把參數(shù)傳給python,然后python計(jì)算的結(jié)果返回給java.于是就寫了一個(gè)工具類.
首先,maven 需要加載jython的依賴.工具類代碼如下:
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.poi.ss.formula.functions.T;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
/**
* @ClassName: JythonUtils
* @Description:TODO(jython 工具類)
* @author: zy
* @date:
*
* @Copyright: 2018 Inc. All rights reserved.
* 注意:
*/
public class JythonUtils {
/**
* @Title: jythonInit
* @Description: TODO(初始化jython)
* @param: @return
* @return: PythonInterpreter
* @throws
*/
public static PythonInterpreter jythonInit(){
//初始化site 配置
Properties props = new Properties();
props.put("python.home", ""); //python Lib 或 jython Lib,根據(jù)系統(tǒng)中該文件目錄路徑
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
//創(chuàng)建PythonInterpreter 對(duì)象
PythonInterpreter interp = new PythonInterpreter();
return interp;
}
/**
* @Title: loadPythonFile
* @Description: TODO(加載python 源碼文件,)
* @param: @param interp
* @param: @param filePath ,比如:F:\\jpython_jar\\jpythonTest\\pythonTest.py 或/testpython/test.py
* @param: @return
* @return: PythonInterpreter
* @throws
*/
public static PythonInterpreter loadPythonFile(PythonInterpreter interp, String filePath){
interp.execfile(filePath);
return interp;
}
/**
* @Title: loadPythonFunc
* @Description: TODO(加載python 源碼文件中的某個(gè)方法)
* @param: @param interp
* @param: @param functionName
* @param: @return
* @return: PyFunction
* @throws
*/
public static PyFunction loadPythonFunc(PythonInterpreter interp, String functionName){
//加載方法
PyFunction func = (PyFunction) interp.get(functionName,PyFunction.class);
return func;
}
/**
* @Title: execFunc
* @Description: TODO(執(zhí)行無(wú)參方法,返回PyObject)
* @param: @param func
* @return: PyObject
* @throws
*/
public static PyObject execFunc(PyFunction func){
PyObject pyobj = func.__call__();
return pyobj;
}
/**
* @Title: execFuncToString
* @Description: TODO(執(zhí)行無(wú)參方法,返回一個(gè)字符串)
* @param: @param func
* @param: @return
* @return: String
* @throws
*/
public static String execFuncToString(PyFunction func){
PyObject pyobj = execFunc(func);
return (String) pyobj.__tojava__(String.class);
}
/**
* @Title: execFuncToString
* @Description: TODO(執(zhí)行有參方法,返回一個(gè)字符串)
* @param: @param func
* @param: @param paramName ,參數(shù)名
* @param: @return
* @return: String
* @throws
*/
public static String execFuncToString2(PyFunction func, String paramName){
PyObject pyobj = func.__call__(new PyString(paramName));
return (String) pyobj.__tojava__(String.class);
}
/**
* @Title: execFuncToInteger
* @Description: TODO(執(zhí)行無(wú)參方法,返回一個(gè)Integer)
* @param: @param func
* @param: @return
* @return: Integer
* @throws
*/
public Integer execFuncToInteger(PyFunction func){
PyObject pyobj = execFunc(func);
return (Integer) pyobj.__tojava__(Integer.class);
}
/**
* @Title: execFuncToList
* @Description: TODO(執(zhí)行無(wú)參方法,返回一個(gè)List)
* @param: @param func
* @param: @return
* @return: List<T>
* @throws
*/
public List<T> execFuncToList(PyFunction func){
PyObject pyobj = execFunc(func);
return (List<T>) pyobj.__tojava__(List.class);
}
/**
* @Title: execFuncToMap
* @Description: TODO(執(zhí)行無(wú)參方法,返回一個(gè)Map<String, Object>)
* @param: @param func
* @param: @return
* @return: Map<String,Object>
* @throws
*/
public Map<String, Object> execFuncToMap(PyFunction func){
PyObject pyobj = execFunc(func);
return (Map<String, Object>) pyobj.__tojava__(Map.class);
}
public void execFuncToByParamsList(PyFunction func, List<T> paramsList){
}
public static void main(String[] args){
PythonInterpreter interp = jythonInit();
//文件名
String filePath = "F:\\jpython_jar\\jpythonTest\\pythonTest.py";
interp = loadPythonFile(interp, filePath);
//函數(shù)名
String functionName = "count";
PyFunction func = loadPythonFunc(interp, functionName);
//執(zhí)行無(wú)參方法,返回PyObject
PyObject pyobj = execFunc(func);
//執(zhí)行無(wú)參方法,返回String
String resultStr = execFuncToString(func);
//執(zhí)行有參方法,返回String
String paramName = "name";
String resultStr2 = execFuncToString2(func, paramName);
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
idea無(wú)法打斷點(diǎn),單擊或雙擊代碼行左側(cè)區(qū)域無(wú)效的解決
這篇文章主要介紹了idea無(wú)法打斷點(diǎn),單擊或雙擊代碼行左側(cè)區(qū)域無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring Security 在 Spring Boot 中的使用詳解【集中式】
這篇文章主要介紹了Spring Security 在 Spring Boot 中的使用【集中式】,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java自定義實(shí)現(xiàn)equals()方法過程解析
這篇文章主要介紹了Java自定義實(shí)現(xiàn)equals()方法過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
關(guān)于idea無(wú)法修改模板中jdk版本問題
這篇文章主要介紹了關(guān)于idea無(wú)法修改模板中jdk版本問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

