當前位置: 首頁>>代碼示例>>Java>>正文


Java PythonInterpreter類代碼示例

本文整理匯總了Java中org.python.util.PythonInterpreter的典型用法代碼示例。如果您正苦於以下問題:Java PythonInterpreter類的具體用法?Java PythonInterpreter怎麽用?Java PythonInterpreter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


PythonInterpreter類屬於org.python.util包,在下文中一共展示了PythonInterpreter類的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())");
    }
}
 
開發者ID:hksoni,項目名稱:SDN-Multicast,代碼行數:29,代碼來源:JythonServer.java

示例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);
	}

}
 
開發者ID:therelaxist,項目名稱:spring-usc,代碼行數:22,代碼來源:PythonRepository.java

示例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;
}
 
開發者ID:Arnauld,項目名稱:gutenberg,代碼行數:24,代碼來源:Lexers.java

示例4: SimulatorImpl

import org.python.util.PythonInterpreter; //導入依賴的package包/類
/**
 * Constructs a new instance.
 */
public SimulatorImpl() {
    // initialize Python interpreter
    Properties properties = new Properties();
    properties.setProperty("python.home", StaticConfiguration.JYTHON_HOME);
    properties.setProperty("python.path", StaticConfiguration.JYTHON_LIB);
    PythonInterpreter.initialize(System.getProperties(), properties, new String[] {""});

    // initialize the shutdown hook to terminate application properly
    Runtime.getRuntime().addShutdownHook(new ShutdownHookThread());

    if (OS.getType() == OS.Type.WINDOWS) {
        // create hidden window to handle WM_CLOSE messages on Windows to react to taskkill
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
    }
}
 
開發者ID:qspin,項目名稱:qtaste,代碼行數:21,代碼來源:SimulatorImpl.java

示例5: 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");
}
 
開發者ID:dream-lab,項目名稱:echo,代碼行數:17,代碼來源:NifiClientFactory.java

示例6: 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;
   }
 
開發者ID:uraplutonium,項目名稱:teabag-ml,代碼行數:25,代碼來源:PyContingencyTable.java

示例7: 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.");
   }
 
開發者ID:uraplutonium,項目名稱:teabag-ml,代碼行數:25,代碼來源:PyDataset.java

示例8: 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());
        }
    }
}
 
開發者ID:apache,項目名稱:qpid-proton-j,代碼行數:26,代碼來源:JythonTest.java

示例9: getCompiledMethod

import org.python.util.PythonInterpreter; //導入依賴的package包/類
/**
 * Get a compiled version of the python call.
 * 
 * @param name
 * @param code
 * @param interp
 * @return
 */
private static synchronized PyObject getCompiledMethod(String name, String code, PythonInterpreter interp) {
	// TODO change this from a synchronized method to a few blocks to
	// improve concurrent performance
	if (objectCache.containsKey(name)) {
		return objectCache.get(name);
	}

	PyObject compiled = interp.compile(code);
	if (objectCache.size() > 25) {
		// keep the size to 6
		objectCache.remove(objectCache.keySet().iterator().next());
	}
	objectCache.put(name, compiled);
	return compiled;
}
 
開發者ID:glaudiston,項目名稱:project-bianca,代碼行數:24,代碼來源:Python.java

示例10: 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);
}
 
開發者ID:tanium,項目名稱:pyjenkins,代碼行數:20,代碼來源:PythonFactoryTest.java

示例11: 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);
}
 
開發者ID:tanium,項目名稱:pyjenkins,代碼行數:23,代碼來源:PythonFactoryTest.java

示例12: testPyJenkinsBuildConfigReviewBuild

import org.python.util.PythonInterpreter; //導入依賴的package包/類
@Test
public void testPyJenkinsBuildConfigReviewBuild() throws Exception {
    BuildContext context = newSimpleBuildContext();
    BuildConfig config = new BuildConfig(true, false);
    PythonInterpreter pyjenkins = PythonFactory.newPyJenkinsInterpreter(context, config);

    String script =
        "from pyjenkins import *\n" +
        "class Foo(PyJenkinsBuild):\n" +
        "  def run(self):\n" +
        "    if not 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);
}
 
開發者ID:tanium,項目名稱:pyjenkins,代碼行數:23,代碼來源:PythonFactoryTest.java

示例13: 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);
}
 
開發者ID:tanium,項目名稱:pyjenkins,代碼行數:23,代碼來源:PythonFactoryTest.java

示例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())");
}
 
開發者ID:vishalshubham,項目名稱:Multipath-Hedera-system-in-Floodlight-controller,代碼行數:25,代碼來源:JythonServer.java

示例15: prepare

import org.python.util.PythonInterpreter; //導入依賴的package包/類
@Setup(Level.Trial)
public void prepare() {
  PythonInterpreter interpreter = new CodeLoader().jython("dispatch");
  dispatcher = (PyFunction) interpreter.get("dispatch");

  MonomorphicState monomorphicState = new MonomorphicState();
  monomorphicState.prepare();
  monomorphicArray = new PyArray(Object.class, monomorphicState.data);

  TriMorphicState triMorphicState = new TriMorphicState();
  triMorphicState.prepare();
  trimorphicArray = new PyArray(Object.class, triMorphicState.data);

  PolyMorphicState polyMorphicState = new PolyMorphicState();
  polyMorphicState.prepare();
  polymorphicArray = new PyArray(Object.class, polyMorphicState.data);
}
 
開發者ID:golo-lang,項目名稱:golo-jmh-benchmarks,代碼行數:18,代碼來源:MethodDispatchMicroBenchmark.java


注:本文中的org.python.util.PythonInterpreter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。