| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package com.coffee.bus.script;
- import cn.hutool.http.webservice.SoapUtil;
- import cn.hutool.json.JSON;
- import cn.hutool.json.JSONUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.python.core.*;
- import org.python.util.PythonInterpreter;
- import org.springframework.stereotype.Component;
- import javax.xml.soap.SOAPMessage;
- import java.util.Properties;
- /**
- * @author lifang
- * @version 1.0.0
- * @ClassName PythonParse.java
- * @Description TODO
- * @createTime 2022年03月28日 10:58:00
- */
- @Slf4j
- @Component
- public class PythonParse implements ScriptParse {
- private final PythonInterpreter interpreter;
- private PyFunction pyFunction;
- public PythonParse() {
- Properties p = new Properties();
- p.setProperty("python.console.encoding", "UTF-8");
- PySystemState systemState = Py.getSystemState();
- PyString xml = new PyString(Thread.currentThread().getContextClassLoader().getResource("python").getPath());
- systemState.path.append(xml);
- PythonInterpreter.initialize(System.getProperties(), p, new String[] {});
- interpreter = new PythonInterpreter();
- }
- @Override
- public String getId() {
- return "python";
- }
- @Override
- public String functionName() {
- return "parse";
- }
- @Override
- public void reset(String script){
- interpreter.exec(Py.newStringUTF8(script));
- pyFunction = interpreter.get(functionName(), PyFunction.class);
- }
- @Override
- public ExecuteResult exec(String param) {
- ExecuteResult executeResult = new ExecuteResult();
- try {
- long start = System.currentTimeMillis();
- PyObject result = pyFunction.__call__(Py.newStringUTF8(param));
- long end = System.currentTimeMillis();
- executeResult.setSuccess(true);
- executeResult.setUseTime(end-start);
- executeResult.setResult( JSONUtil.parse(result));
- }catch (Exception e){
- executeResult.setSuccess(false);
- executeResult.setException(e);
- }
- return executeResult;
- }
- public static void main(String[] args){
- Properties p = new Properties();
- p.setProperty("python.console.encoding", "UTF-8");
- PySystemState systemState = Py.getSystemState();
- PyString xml = new PyString("E:\\software\\coffee-boot\\coffee-admin\\src\\main\\resources\\python");
- // PyString xml = new PyString("_classpath_:python");
- systemState.path.append(xml);
- PythonInterpreter.initialize(System.getProperties(), p, new String[] {});
- PythonInterpreter pythonInterpreter = new PythonInterpreter();
- String str = "# -*- coding: utf-8 -*-\n" +
- "import sys\n" +
- "reload(sys)\n" +
- "sys.setdefaultencoding('utf-8')\n" +
- "import json\n" +
- "import xmltodict as xmltodict\n" +
- "\n" +
- "\n" +
- "def parse(str):\n" +
- "\n" +
- " json_result={}\n" +
- " json_obj= json.loads(str)\n" +
- "\n" +
- " json_result['1']=json_obj['name']\n" +
- " return json_result";
- pythonInterpreter.exec(Py.newStringUTF8(str));
- PyFunction parse = pythonInterpreter.get("parse", PyFunction.class);
- PyObject pyObject = parse.__call__(Py.newStringUTF8( "{\"name\": \"小明\", \"age\": \"12\"}"));
- JSON parse1 = JSONUtil.parse(pyObject);
- pythonInterpreter.close();
- System.out.println(pyObject);
- System.out.println(parse1);
- }
- }
|