本文整理汇总了Java中org.python.util.PythonInterpreter.exec方法的典型用法代码示例。如果您正苦于以下问题:Java PythonInterpreter.exec方法的具体用法?Java PythonInterpreter.exec怎么用?Java PythonInterpreter.exec使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.python.util.PythonInterpreter
的用法示例。
在下文中一共展示了PythonInterpreter.exec方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
if (this.host == null) {
p.exec("run_server(port=" + this.port + ", locals=locals())");
} else {
p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
}
}
示例2: initializeInterperter
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public void initializeInterperter(PythonInterpreter interpreter)
{
boolean localsUninitialized = interpreter.getLocals() == initialLocals;
if(localsUninitialized)
{
PyStringMap locals = new PyStringMap();
interpreter.setLocals(locals);
interpreter.exec(scripts.get(PythonTransformationHelper.getImportStatements()));
interpreter.exec(scripts.get(PythonTransformationHelper.getGetValueDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getIsEmptyDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getHasSelectedRowsStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getGetValueFromNestedColumnByIndexDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getRowIndexDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getVDefStatement()));
}
if(localsUninitialized ||(!libraryHasBeenLoaded || reloadLibrary))
{
importUserScripts(interpreter);
}
}
示例3: loadAvailableLexers
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public List<LexerInfo> loadAvailableLexers(PyGateway gateway) {
PythonInterpreter interpreter = gateway.getInterpreter();
// Simple use Pygments as you would in Python
interpreter.exec(""
+ "from pygments.lexers import get_all_lexers\n"
+ "result = get_all_lexers()");
PyGenerator result = (PyGenerator) interpreter.get("result");
ArrayList<LexerInfo> infos = Lists.newArrayList();
for (Object o : result) {
PyTuple tuple = (PyTuple) o;
String name = (String) tuple.get(0);
List<String> aliases = Lists.newArrayListWithCapacity(3);
for (Object alias : (PyTuple) tuple.get(1)) {
String str = (String) alias;
aliases.add(str);
}
LexerInfo info = new LexerInfo(name, aliases);
infos.add(info);
}
return infos;
}
示例4: NifiClientFactory
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public NifiClientFactory() {
/*StringBuilder sb = new StringBuilder();
sb.append(System.getProperty("java.class.path"));
sb.append(":").append("~/workplace/echo/src/main/nifi");
Properties props = new Properties();
props.setProperty("python.path", sb.toString());
props.setProperty("python.verbose", "error");
PythonInterpreter.initialize(System.getProperties(), props, new String[0]);*/
PythonInterpreter interpreter = new PythonInterpreter();
//interpreter.exec("import os\nprint os.getcwd()");
interpreter.exec("import sys\nif '' not in sys.path:\n sys.path.append(\"\")");
interpreter.exec("from modules.json2pojo.src.main.python.DefaultNifiClient import DefaultNifiClient");
nifiClientClass = interpreter.get("DefaultNifiClient");
}
示例5: testPyJenkinsInterpreter
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Test
public void testPyJenkinsInterpreter() throws Exception {
BuildContext context = newSimpleBuildContext();
PythonInterpreter pyjenkins = PythonFactory.newPyJenkinsInterpreter(context, newSimpleBuildConfig());
String script =
"from pyjenkins import *\n" +
"class Foo(PyJenkinsBuild):\n" +
" def run(self):\n" +
" return Result.NOT_BUILT\n" +
"register_pybuild( Foo() )";
pyjenkins.exec(script);
PyJenkinsBuild pybuild = context.pybuild;
assertNotNull("should have registered pybuild", pybuild);
Result result = pybuild.run();
assertEquals("should return the specified result", Result.NOT_BUILT, result);
}
示例6: testPyJenkinsBuildConfigFalse
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Test
public void testPyJenkinsBuildConfigFalse() throws Exception {
BuildContext context = newSimpleBuildContext();
BuildConfig config = new BuildConfig(false, false);
PythonInterpreter pyjenkins = PythonFactory.newPyJenkinsInterpreter(context, config);
String script =
"from pyjenkins import *\n" +
"class Foo(PyJenkinsBuild):\n" +
" def run(self):\n" +
" if is_pull_request_build() or is_release_build():\n" +
" return Result.FAILURE\n" +
" return Result.SUCCESS\n" +
"register_pybuild( Foo() )";
pyjenkins.exec(script);
PyJenkinsBuild pybuild = context.pybuild;
assertNotNull("should have registered pybuild", pybuild);
Result result = pybuild.run();
assertEquals("should return the specified result", Result.SUCCESS, result);
}
示例7: testPyJenkinsBuildConfigReleaseBuild
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Test
public void testPyJenkinsBuildConfigReleaseBuild() throws Exception {
BuildContext context = newSimpleBuildContext();
BuildConfig config = new BuildConfig(false, true);
PythonInterpreter pyjenkins = PythonFactory.newPyJenkinsInterpreter(context, config);
String script =
"from pyjenkins import *\n" +
"class Foo(PyJenkinsBuild):\n" +
" def run(self):\n" +
" if is_pull_request_build() or not is_release_build():\n" +
" return Result.FAILURE\n" +
" return Result.SUCCESS\n" +
"register_pybuild( Foo() )";
pyjenkins.exec(script);
PyJenkinsBuild pybuild = context.pybuild;
assertNotNull("should have registered pybuild", pybuild);
Result result = pybuild.run();
assertEquals("should return the specified result", Result.SUCCESS, result);
}
示例8: Interpretor
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public Interpretor() {
PythonInterpreter interpreter = new PythonInterpreter();
String dirpathString = ServletContextParameterMap.getParameterValue(ContextParameter.WEBAPP_PATH) +
"/" + ServletContextParameterMap
.getParameterValue(ContextParameter.PYTHON_SCRIPTS_DIRECTORY);
;
if(dirpathString == null || dirpathString.toString().length() <= 1) {
dirpathString = "../karma-web/src/main/webapp/resources/pythonCleaningscripts";
}
logger.info("Setting Python Scripts Directory for karma-cleaning: " + dirpathString);
interpreter.exec("import sys");
// /Users/bowu/projects/IDCT/src/edu/isi/karma/cleaning
interpreter.exec("sys.path.append('" + dirpathString + "')");
interpreter.exec("from FunctionList import *");
interpreter.exec("from Interpreter import *");
// interpreter.exec("print sys.path");
interpreterClass = interpreter.get("Interpreter");
}
示例9: test_jython_loading
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Test
public void test_jython_loading() throws Throwable {
CodeLoader loader = new CodeLoader();
PythonInterpreter interpreter = loader.jython("check");
PyObject object = interpreter.get("truth");
assertTrue(object instanceof PyFunction);
PyFunction fun = (PyFunction) object;
assertEquals(new PyInteger(42), fun.__call__());
object = interpreter.get("incr");
assertTrue(object instanceof PyFunction);
fun = (PyFunction) object;
assertEquals(new PyInteger(3), fun.__call__(new PyInteger(2)));
fun = (PyFunction) interpreter.get("foo");
interpreter.exec("from org.gololang.microbenchmarks.support import Foo");
PyObject foo = interpreter.eval("Foo()");
assertEquals(new PyString("foo"), fun.__call__(foo));
}
示例10: 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 );
}
}
示例11: RunButtonActionPerformed
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
private void RunButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RunButtonActionPerformed
this.PythonOutput.setText("");
String python = this.PythonText.getText();
//python.replaceAll("\t", " ");
/*
try {
File f = new File("filename.txt") ;
PrintWriter out = new PrintWriter(f);
out.write(python);
out.close();
// open it
Desktop.getDesktop().open(f) ;
} catch (Exception ex) {
ex.printStackTrace();
}
*/
PythonInterpreter interp = new PythonInterpreter();
interp.setIn(System.in);
interp.setOut(System.out);
interp.exec(python);
}
示例12: apply
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Override
public ConstructionList apply( ConstructionList parameters, AttributeMap attrs, ConstructionChanges effects )
throws Command.Failure
{
ConstructionList result = new ConstructionList();
if ( parameters .size() != 1 )
throw new Failure( "start parameter must be a single connector" );
Construction c = parameters .get( 0 );
if ( ! ( c instanceof Point ) )
throw new Failure( "start parameter must be a connector" );
Point pt1 = (Point) c;
String script = (String) attrs .get( SCRIPT_ATTR );
final Symmetry symm = (Symmetry) attrs .get( CommandTransform.SYMMETRY_GROUP_ATTR_NAME );
ZomicVirtualMachine builder = new ZomicVirtualMachine( pt1, effects, symm );
PythonInterpreter interp = new PythonInterpreter();
interp .set( "zomicVM", builder );
interp .exec( script );
result .addConstruction( builder .getLastPoint() );
return result;
}
示例13: getJavaObjectFromJythonFile
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public Object getJavaObjectFromJythonFile(String interfaceName, String pathToJythonModule)
{
Object javaObject = null;
PythonInterpreter interpreter = cacheInterpreter(pathToJythonModule);
String tempName = pathToJythonModule.substring(pathToJythonModule.lastIndexOf("/") + 1);
tempName = tempName.substring(0, tempName.indexOf("."));
System.out.println(tempName);
String instanceName = tempName.toLowerCase();
String javaClassName = tempName.substring(0, 1).toUpperCase() + tempName.substring(1);
String objectDef = "=" + javaClassName + "()";
interpreter.exec(instanceName + objectDef);
try
{
Class<?> JavaInterface = Class.forName(interfaceName);
javaObject = interpreter.get(instanceName).__tojava__(JavaInterface);
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace(); // Add logging here36.
}
return javaObject;
}
示例14: run
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
* The main thread for this class invoked by Thread.run()
*
* @see java.lang.Thread#run()
*/
public void run() {
PythonInterpreter p = new PythonInterpreter();
for (String name : this.locals.keySet()) {
p.set(name, this.locals.get(name));
}
URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
String jarPath = jarUrl.getPath();
if (jarUrl.getProtocol().equals("file")) {
// If URL is of type file, assume that we are in dev env and set path to python dir.
// else use the jar file as is
jarPath = jarPath + "../../src/main/python/";
}
p.exec("import sys");
p.exec("sys.path.append('" + jarPath + "')");
p.exec("from debugserver import run_server");
p.exec("run_server(" + this.port + ", '0.0.0.0', locals())");
}
示例15: main
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public static void main(String... args) {
PythonInterpreter pythonInterpreter = new PythonInterpreter();
int count = 0;
for(String arg : args) {
try {
count++;
pythonInterpreter.set("number" + count, new Integer(arg));
}
catch (NumberFormatException e) {
System.err.println(arg + " is not a number!");
count--;
}
}
pythonInterpreter.set("sum", 0);
for(int i = 1; i <= count; i++) {
pythonInterpreter.exec(String.format("sum += %s", "number"+i));
}
PyObject sum = pythonInterpreter.get("sum");
System.out.println("The result is: " + sum.toString());
}