com.datastax.util.scipt.PythonRunner Maven / Gradle / Ivy
The newest version!
package com.datastax.util.scipt;
import com.datastax.util.lang.ProcessUtil;
import java.io.IOException;
public class PythonRunner {
public static void main(String[] args) throws IOException {
pythonRun("/home/dataexa/Workspace/Python/TensorFlow/TensorFlow-Tutorials/02_logistic_regression.py");
}
/*static {
Properties props = new Properties();
props.put("python.home", "/usr/bin/python");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PySystemState sys = Py.getSystemState();
//sys.path.add("/usr/local/lib/python2.7/dist-packages/tensorflow");
PythonInterpreter.initialize(preprops, props, new String[0]);
}*/
public static void pythonRun(String pyFile,String...parameters){
ProcessUtil processUtil=new ProcessUtil();
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("python "+pyFile);
if(parameters!=null && parameters.length>0){
for(String param : parameters){
stringBuffer.append(" "+param);
}
}
processUtil.execute(stringBuffer.toString());
for(String line : processUtil.getStdoutList()){
System.out.println(line);
}
}
/*public static void pythonExecute(String filePath) throws IOException {
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
pin.execfile(is);
is.close();
}
*//**
* 获取python程序的变量值
* @param filePath
* @param ponames
* @return
* @throws IOException
*//*
public static List transP2JData(String filePath, String...ponames) throws IOException{
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
pin.execfile(is);
is.close();
List pos = new ArrayList<>();
for (String poname : ponames) {
PyObject po = pin.get(poname);
pos.add(po);
}
return pos;
}
*//**
* 将参数赋给python程序执行
* @param filePath
* @param pomaps
* @throws IOException
*//*
public static void transJ2PData(String filePath, Map pomaps) throws IOException {
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
for (String pomapkey : pomaps.keySet()) {
pin.set(pomapkey, pomaps.get(pomapkey));
}
pin.execfile(is);
is.close();
}
*//**
* 将参数赋给python程序执行,并获取python中的变量值
* @param filePath
* @param pomaps
* @param ponames
* @return
* @throws IOException
*//*
public static List transJ2PData(String filePath, Map pomaps, String...ponames) throws IOException {
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
for (String pomapkey : pomaps.keySet()) {
pin.set(pomapkey, pomaps.get(pomapkey));
}
pin.execfile(is);
is.close();
List pos = new ArrayList<>();
for (String poname : ponames) {
PyObject po = pin.get(poname);
pos.add(po);
}
return pos;
}*/
}