package com.jython.utils;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.python.core.PyDictionary;
import org.python.core.PyFloat;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyList;
import org.python.core.PyLong;
import org.python.core.PyNone;
import org.python.core.PyObject;
import org.python.core.PySet;
import org.python.core.PyString;
import org.python.core.PyType;
import org.python.util.PythonInterpreter;
public class JythonUtils {
/**
* 将PyObject对象转成String对象
*
* @author ZQ
* @param pyObject
* @return
* @throws Exception
*/
public static Object toObjectFromPyObject(PyObject pyObject)
throws Exception {
if (pyObject instanceof PyString)
return (String) pyObject.__tojava__(String.class);
else if (pyObject instanceof PyList)
return (List<?>) pyObject.__tojava__(List.class);
else if (pyObject instanceof PyInteger)
return (int) pyObject.__tojava__(Integer.class);
else if (pyObject instanceof PyFloat)
return (float) pyObject.__tojava__(Float.class);
else if (pyObject instanceof PyLong)
return (long) pyObject.__tojava__(Long.class);
else if (pyObject instanceof PyDictionary)
return (Map<?, ?>) pyObject.__tojava__(Map.class);
else if (pyObject instanceof PySet)
return (Set<?>) pyObject.__tojava__(Set.class);
else if (pyObject instanceof PyNone)
return null;
else
throw new Exception("pyObject必须是基本类型之一");
}
/**
* 解决中文乱码问题
*
* @param str
* @return
* @throws UnsupportedEncodingException
*/
public static String encoding(String str)
throws UnsupportedEncodingException {
return new String(str.getBytes("iso8859-1"), "utf-8");
}
/**
* @author ZQ
* @param filename
* 文件名
* @param funName
* 函数名
* @param args
* 可变参数
* @return
* @throws Exception
*/
public static PyObject execPyFunction(String filename, String funName,
Object... args) throws Exception {
int len = args.length;
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(filename);
PyFunction function = interpreter.get(funName, PyFunction.class);
if(len==0)
return function.__call__();
PyObject[] pyObjects = new PyObject[len];
for (int i = 0; i < pyObjects.length; i++) {
if (args[i] instanceof Integer){
pyObjects[i] = new PyInteger((int) args[i]);
continue;
}
else if (args[i] instanceof Float){
pyObjects[i] = new PyFloat((float) args[i]);
continue;
}
else if (args[i] instanceof Long){
pyObjects[i] = new PyLong((Long) args[i]);
continue;
}
else if (args[i] instanceof String){
pyObjects[i] = new PyString((String) args[i]);
continue;
}
else if (args[i] instanceof Character){
pyObjects[i] = new PyString((char) args[i]);
continue;
}
else if (args[i] instanceof List<?>) {
PyList pyList = new PyList(PyType.fromClass(PyInteger.class));
for (Object o : (List<?>) args[i]) {
if (o instanceof Integer){
pyList.append(new PyInteger((int) o));
continue;
}
else if (o instanceof Float){
pyList.append(new PyFloat((Float) o));
continue;
}
else if (o instanceof Long){
pyList.append(new PyLong((Long) o));
continue;
}
else if (o instanceof String){
pyList.append(new PyString((String) o));
continue;
}
else
throw new Exception(
"java中List中的子元素只能是int,float,long,String中的!");
}
pyObjects[i] = pyList;
continue;
} else
throw new Exception("参数类型只能是int,float,long,String,List其中的!");
}
return function.__call__(pyObjects);
}
/**执行py文件
* @author ZQ
* @param filename
*/
public static void execPyFile(String filename){
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile(filename);
}
}
评论2