您当前的位置: 首页 >  Python

庄小焱

暂无认证

  • 2浏览

    0关注

    805博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java——java服务调用python脚本服务

庄小焱 发布时间:2021-09-29 23:28:04 ,浏览量:2

摘要

在日常的开发过程中我们经常遇到的java调用python的脚本,所以在本博文中我将为大家展示java项目的dokcer调用python服务的docker项目,同样在java调用其他语言的时候也是同样的一个道理。以下的例子仅仅工大家参考。

项目结构

项目代码
package com.xjl.javatopython.function;

import org.python.core.*;
import org.python.util.PythonInterpreter;

/**
 * @Classname JavaCallPython
 * @Description TODO
 * @Date 2021/9/30 7:31
 * @Created by xjl
 */
public class JavaCallPython {
    //解释器
    private PythonInterpreter pythonInterpreter;

    public PythonInterpreter getPythonInterpreter(String pythonfile) {
        PythonInterpreter pythonInterpreter = new PythonInterpreter();
        pythonInterpreter.execfile(pythonfile);
        return pythonInterpreter;
    }

    public void setPythonInterpreter(PythonInterpreter pythonInterpreter) {
        this.pythonInterpreter = pythonInterpreter;
    }

    /**
     * @description TODO   调用py文件里的属性
     * @param: interpreter
     * @param: path
     * @date: 2021/9/30 6:29
     * @return: void
     * @author: xjl
     */
    public void calllpython_name(PythonInterpreter interpreter) {
        PyObject xyzobj = interpreter.get("xyz");
        System.out.println("xyz=" + xyzobj);
        PyObject abcobj = interpreter.get("abc");
        System.out.println("abc=" + abcobj);
        interpreter.set("newstr", "newString");//手动添加newstr变量
        System.out.println("newstr=" + interpreter.get("newstr"));
        interpreter.exec("print('='*50)");
    }

    /**
     * @description TODO 调用python的函数
     * @param: interpreter
     * @param: path
     * @date: 2021/9/30 6:30
     * @return: void
     * @author: xjl
     */
    public void calllpython_function(PythonInterpreter interpreter) {
        PyFunction funStr = interpreter.get("func_str", PyFunction.class);
        System.out.println(funStr.__call__().__tojava__(PyString.class));
        System.out.println(funStr.__call__());
        PyFunction funList = interpreter.get("func_list", PyFunction.class);
        PyList list = (PyList) funList.__call__().__tojava__(PyList.class);
        System.out.println(list);
        System.out.println(list.get(2));
        PyFunction funDict = interpreter.get("func_dict", PyFunction.class);
        PyDictionary dict = (PyDictionary) funDict.__call__().__tojava__(PyDictionary.class);
        System.out.println(dict);
        System.out.println(dict.get(2));
    }

    public static void main(String[] args) {
        String python_path = "Java_To_Python/src/main/resources/scripts/test.py";
        JavaCallPython testCallPython_jython = new JavaCallPython();
        PythonInterpreter pythonInterpreter = testCallPython_jython.getPythonInterpreter(python_path);
        testCallPython_jython.calllpython_name(pythonInterpreter);
        testCallPython_jython.calllpython_function(pythonInterpreter);
    }
}
#!/usr/bin/env python3
# coding=utf-8

xyz='hello python'
abc=1234

class Result:
    s = ''
    r = 0.0
    def __init__(self, s, r):
        self.s = s
        self.r = r

def func_cls(x, y):
    print('py fun=x^2+2xy+y^2')
    print('x={0}, y={1}'.format(x, y))
    return Result('pyResult', eval('x**2 + 2*x*y + y**2'))

def func_str():
    return 'func2 return str'

def func_list():
    return [11, 22, 33]

def func_dict():
    return {1:'a', 2:'b', 3:'c'}

#没有变量newstr
def func_nostr():
    return newstr

if __name__ == '__main__':
    print('py main run')
#   func_nostr()#NameError: name 'newstr' is not defined

关注
打赏
1657692713
查看更多评论
立即登录/注册

微信扫码登录

0.0371s