本文整理汇总了Java中org.python.util.PythonInterpreter.execfile方法的典型用法代码示例。如果您正苦于以下问题:Java PythonInterpreter.execfile方法的具体用法?Java PythonInterpreter.execfile怎么用?Java PythonInterpreter.execfile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.python.util.PythonInterpreter
的用法示例。
在下文中一共展示了PythonInterpreter.execfile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: CreateFromPython
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public void CreateFromPython()
{
PythonInterpreter interpreter = new PythonInterpreter(null, new PySystemState());
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString(context.settings.GetModelPath()));
if(context.settings.GetSysPath() != null)
sys.path.append(new PyString(context.settings.GetSysPath()));
/*
// some magic to pass context to all python modules
interpreter.set("context", context);
interpreter.set("model_service", model_service);
interpreter.exec("import __builtin__");
interpreter.exec("__builtin__.context = context");
interpreter.exec("__builtin__.model_service = model_service");*/
interpreter.execfile(context.settings.GetModelPath() + '/' + context.settings.GetModelMain());
interpreter.cleanup();
UpdateDefinedSensors();
}
示例2: getCount
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public double getCount(int[] query) {
// convert the int[] query to PyList<PyInteger> pyQuery
// the input java query is like [0, 2, 1, -1], the converted python query is like [1, 3, 2]
List<PyInteger> pyQueryList = new ArrayList<PyInteger>();
for (int eachValue : query)
if (eachValue != -1)
pyQueryList.add(new PyInteger(eachValue+1));
PyList pyQuery = new PyList(pyQueryList);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/media/uraplutonium/Workstation/Workspace/teabag-ml/src/main/java/teabagml/adtree/PySparseADTree.py");
PyFunction getCountFunc = (PyFunction)interpreter.get("getCount", PyFunction.class);
PyInteger pyCount = (PyInteger)(getCountFunc.__call__(contab, pyQuery));
double count = (double)(pyCount.getValue());
/*
System.out.print("Query: ");
for (int eachValue : query) {
System.out.print(eachValue + ' ');
}
System.out.println("\t Count: " + count);
*/
return count;
}
示例3: PyDataset
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public PyDataset(String filePath, boolean symbolic, boolean labelled) {
// BEWEAR: symbolic and labelled are NOT used
this.symbolic = symbolic;
this.labelled = labelled;
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/media/uraplutonium/Workstation/Workspace/teabag-ml/src/main/java/teabagml/adtree/PySparseADTree.py");
PyFunction loadFileFunc = (PyFunction)interpreter.get("loadFile", PyFunction.class);
fileTuple = (PyTuple)(loadFileFunc.__call__(new PyString(filePath)));
// todo extract dimension, dataSize, arity from fileTuple
PyInteger pyArityLength = (PyInteger)(fileTuple.pyget(1));
PyList pyArityList = (PyList)(fileTuple.pyget(2));
PyInteger pyRecordsLength = (PyInteger)(fileTuple.pyget(3));
this.dimension = pyArityLength.getValue();
this.dataSize = pyRecordsLength.getValue();
PyObject[] pyArityArray = pyArityList.getArray();
String[] arityNames = new String[dimension];
int[] arityArray = new int[dimension];
for (int i=0; i<dimension; i++) {
arityArray[i] = ((PyInteger)(pyArityArray[i])).getValue();
arityNames[i] = Integer.toString(arityArray[i]);
}
arity = new Arity(dimension, arityNames, arityArray);
System.out.println("file loaded.");
}
示例4: runTestOnce
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
private void runTestOnce(String testScript, PythonInterpreter interp, int invocationsSoFar)
{
try
{
interp.execfile(testScript);
}
catch (PyException e)
{
if( e.type.toString().equals("<type 'exceptions.SystemExit'>") && e.value.toString().equals("0") )
{
// Build succeeded.
}
else
{
if (LOGGER.isLoggable(Level.FINE))
{
LOGGER.log(Level.FINE, "Jython interpreter failed. Test failures?", e);
}
// This unusual code is necessary because PyException toString() contains the useful Python traceback
// and getMessage() is usually null
fail("Caught PyException on invocation number " + invocationsSoFar + ": " + e.toString() + " with message: " + e.getMessage());
}
}
}
示例5: execute
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public void execute( ExecutionContext executionContext ) throws ParsingException, ExecutionException
{
PythonInterpreter pythonInterpreter = adapter.getPythonInterpreter( executionContext, executable );
try
{
PyCode pythonCode = this.pythonCode;
if( pythonCode != null )
pythonInterpreter.exec( pythonCode );
else
// We're using a stream because PythonInterpreter does not
// expose a string-based method that also accepts a filename.
pythonInterpreter.execfile( new ByteArrayInputStream( sourceCode.getBytes() ), executable.getDocumentName() );
}
catch( Exception x )
{
throw JythonAdapter.createExecutionException( executable.getDocumentName(), startLineNumber, x );
}
finally
{
JythonAdapter.flush( pythonInterpreter, executionContext );
}
}
示例6: importUserScripts
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public void importUserScripts(PythonInterpreter interpreter) {
String dirpathString = ServletContextParameterMap
.getParameterValue(ContextParameter.USER_PYTHON_SCRIPTS_DIRECTORY);
if (dirpathString != null && dirpathString.compareTo("") != 0) {
File f = new File(dirpathString);
String[] scripts = f.list(new FilenameFilter(){
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".py");
}});
for(String script : scripts)
{
interpreter.execfile(dirpathString + File.separator + script);
}
}
}
示例7: PythonExecutor
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public PythonExecutor(Object extension) throws PythonWrapperError {
String scriptPath = getScriptPath(extension);
pinterp = new PythonInterpreter();
pinterp.exec("import sys");
String modulePath = new File(scriptPath).getParent();
modulePath = modulePath.replace("\\", "\\\\");
modulePath = modulePath.replace("'", "\\'");
String cmdPath = "sys.path.append('" + modulePath + "')";
pinterp.exec(cmdPath);
pinterp.execfile(scriptPath);
pinterp.set("extension", extension);
callId = 0;
this.extension = extension;
if (hasFunction("init_plugin", 0)) {
pinterp.exec("init_plugin()");
}
}
示例8: hiveColumnParserTest
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Test
public void hiveColumnParserTest() {
LaunchJython l = new LaunchJython();
PythonInterpreter interpreter = l.setUp();
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("jython-test/HiveColumnParserTest.py");
interpreter.execfile(inputStream);
}
示例9: avroTest
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Test
public void avroTest() {
LaunchJython l = new LaunchJython();
PythonInterpreter interpreter = l.setUp();
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("jython-test/AvroColumnParserTest.py");
interpreter.execfile(inputStream);
}
示例10: onReceive
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Override
public void onReceive(Object o) throws Exception {
if (o instanceof String) {
Properties whProps = EtlJobPropertyDao.getWherehowsProperties();
PyDictionary config = new PyDictionary();
for (String key : whProps.stringPropertyNames()) {
String value = whProps.getProperty(key);
config.put(new PyString(key), new PyString(value));
}
sys = new PySystemState();
sys.argv.append(config);
interpreter = new PythonInterpreter(null, sys);
String msg = (String) o;
Logger.info("Start build {} tree", msg);
InputStream in = null;
switch (msg) {
case "dataset":
in = EtlJob.class.getClassLoader().getResourceAsStream("jython/DatasetTreeBuilder.py");
break;
case "flow":
//in = EtlJob.class.getClassLoader().getResourceAsStream("jython/FlowTreeBuilder.py");
break;
default:
Logger.error("unknown message : {}", msg);
}
if (in != null) {
interpreter.execfile(in);
in.close();
Logger.info("Finish build {} tree", msg);
} else {
Logger.error("can not find jython script");
}
} else {
throw new Exception("message type is not supported!");
}
}
示例11: PySparseADTree
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public PySparseADTree(String dataPath) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/media/uraplutonium/Workstation/Workspace/teabag-ml/src/main/java/teabagml/adtree/PySparseADTree.py");
PyFunction makeSparseADTreeFunc = (PyFunction)interpreter.get("makeSparseADTree", PyFunction.class);
adtree = makeSparseADTreeFunc.__call__(new PyString(dataPath));
System.out.println("sparse adtree constructed.");
}
示例12: PyContingencyTable
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public PyContingencyTable(PySparseADTree pyADTree, int[] attrList) {
// convert the int[] attrList to PyList<PyInteger> pyAttrList
PyInteger[] pyAttrArray = new PyInteger[attrList.length];
for (int i=0; i<attrList.length; i++)
pyAttrArray[i] = new PyInteger(attrList[i]);
PyList pyAttrList = new PyList(pyAttrArray);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/media/uraplutonium/Workstation/Workspace/teabag-ml/src/main/java/teabagml/adtree/PySparseADTree.py");
PyFunction makeContabFunc = (PyFunction)interpreter.get("makeContab", PyFunction.class);
contab = makeContabFunc.__call__(pyADTree.getPyADTree(), pyAttrList);
System.out.println("contingency table constructed.");
}
示例13: PyADTreeDataset
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public PyADTreeDataset(String filePath, boolean symbolic, boolean labelled) {
// BEWEAR: symbolic and labelled are NOT used
if (pyADTree == null) {
long startTime = System.currentTimeMillis();
//DataCollector.startEvent("adtree", startTime);
this.symbolic = symbolic;
this.labelled = labelled;
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/media/uraplutonium/Workstation/Workspace/teabag-ml/src/main/java/teabagml/adtree/PySparseADTree.py");
PyFunction makeSparseADTreeFunc = (PyFunction)interpreter.get("makeSparseADTree", PyFunction.class);
adtreeTuple = (PyTuple)(makeSparseADTreeFunc.__call__(new PyString(filePath)));
// todo extract dimension, dataSize, arity from adtreeTuple
PyADTreeDataset.pyADTree = (PyObject)(adtreeTuple.pyget(0));
PyInteger pyArityLength = (PyInteger)(adtreeTuple.pyget(1));
PyList pyArityList = (PyList)(adtreeTuple.pyget(2));
PyInteger pyRecordsLength = (PyInteger)(adtreeTuple.pyget(3));
this.dimension = pyArityLength.getValue();
this.dataSize = pyRecordsLength.getValue();
PyObject[] pyArityArray = pyArityList.getArray();
String[] arityNames = new String[dimension];
int[] arityArray = new int[dimension];
for (int i=0; i<dimension; i++) {
arityArray[i] = ((PyInteger)(pyArityArray[i])).getValue();
arityNames[i] = Integer.toString(arityArray[i]);
}
arity = new Arity(dimension, arityNames, arityArray);
System.out.println("sparse adtree constructed.");
//DataCollector.endEvent("adtree", startTime);
} else {
System.out.println("sparse adtree already built.");
}
// initialise the contabMap
contabMap = new HashMap<String, PyObject>();
}
示例14: getContab
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
*
*/
private PyObject getContab(int[] attrList) {
String attrStr = "";
for (int eachAttr : attrList) {
attrStr = attrStr + Integer.toString(eachAttr) + ",";
}
if (contabMap.containsKey(attrStr)) {
// contab already built
System.out.println("contingency table already exists.");
return contabMap.get(attrStr);
} else {
// build new contab and add it to contabMap
// convert the int[] attrList to PyList<PyInteger> pyAttrList
PyInteger[] pyAttrArray = new PyInteger[attrList.length];
for (int i=0; i<attrList.length; i++)
pyAttrArray[i] = new PyInteger(attrList[i]);
PyList pyAttrList = new PyList(pyAttrArray);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/media/uraplutonium/Workstation/Workspace/teabag-ml/src/main/java/teabagml/adtree/PySparseADTree.py");
PyFunction makeContabFunc = (PyFunction)interpreter.get("makeContab", PyFunction.class);
PyObject contab = makeContabFunc.__call__(PyADTreeDataset.pyADTree, pyAttrList);
contabMap.put(attrStr, contab);
System.out.println("contingency table constructed.");
return contab;
}
}
示例15: getTokens
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
* Return list of token for command.
* @param command The command
* @return Token list
* @throws java.io.UnsupportedEncodingException
*/
public PyList getTokens(String command) throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
sb.append("import cStringIO");
sb.append("\n");
sb.append("import tokenize");
sb.append("\n");
sb.append("command = str('");
sb.append(command);
sb.append("')");
sb.append("\n");
sb.append("f = cStringIO.StringIO(command)");
sb.append("\n");
sb.append("tokens = []");
sb.append("\n");
sb.append("def eater(*args):");
sb.append("\n");
sb.append(" tokens.append(args)");
sb.append("\n");
sb.append("tokenize.tokenize_loop(f.readline, eater)");
sb.append("\n");
String code = sb.toString();
String encoding = "utf-8";
PythonInterpreter pi = new PythonInterpreter();
try {
pi.execfile(new ByteArrayInputStream(code.getBytes(encoding)));
PyList tokens = (PyList)pi.get("tokens");
return tokens;
} catch (Exception e){
return null;
}
}