您好,登錄后才能下訂單哦!
最近有個功能需要java與python之間的數(shù)據(jù)交互,java需要把參數(shù)傳給python,然后python計算的結(jié)果返回給java.于是就寫了一個工具類.
首先,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 對象 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 源碼文件中的某個方法) * @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í)行無參方法,返回PyObject) * @param: @param func * @return: PyObject * @throws */ public static PyObject execFunc(PyFunction func){ PyObject pyobj = func.__call__(); return pyobj; } /** * @Title: execFuncToString * @Description: TODO(執(zhí)行無參方法,返回一個字符串) * @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í)行有參方法,返回一個字符串) * @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í)行無參方法,返回一個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í)行無參方法,返回一個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í)行無參方法,返回一個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í)行無參方法,返回PyObject PyObject pyobj = execFunc(func); //執(zhí)行無參方法,返回String String resultStr = execFuncToString(func); //執(zhí)行有參方法,返回String String paramName = "name"; String resultStr2 = execFuncToString2(func, paramName); } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對億速云的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。