当前位置: 首页>>代码示例>>Java>>正文


Java PythonInterpreter.get方法代码示例

本文整理汇总了Java中org.python.util.PythonInterpreter.get方法的典型用法代码示例。如果您正苦于以下问题:Java PythonInterpreter.get方法的具体用法?Java PythonInterpreter.get怎么用?Java PythonInterpreter.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.python.util.PythonInterpreter的用法示例。


在下文中一共展示了PythonInterpreter.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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

示例2: 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

示例3: 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

示例4: 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

示例5: 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

示例6: 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));
}
 
开发者ID:golo-lang,项目名称:golo-jmh-benchmarks,代码行数:21,代码来源:CodeLoaderTest.java

示例7: enter

import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Override
public Object enter( String entryPointName, Executable executable, ExecutionContext executionContext, Object... arguments ) throws NoSuchMethodException, ParsingException, ExecutionException
{
	entryPointName = toPythonStyle( entryPointName );
	PythonInterpreter pythonInterpreter = (PythonInterpreter) executionContext.getAttributes().get( JYTHON_INTERPRETER );
	Py.setSystemState( pythonInterpreter.getSystemState() );
	PyObject method = pythonInterpreter.get( entryPointName );
	if( method == null )
		throw new NoSuchMethodException( entryPointName );
	try
	{
		PyObject[] pythonArguments = Py.javas2pys( arguments );
		PyObject r = method.__call__( pythonArguments );
		return r.__tojava__( Object.class );
	}
	catch( Exception x )
	{
		throw JythonAdapter.createExecutionException( executable.getDocumentName(), 0, x );
	}
	finally
	{
		flush( pythonInterpreter, executionContext );
	}
}
 
开发者ID:tliron,项目名称:scripturian,代码行数:25,代码来源:JythonAdapter.java

示例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");
}
 
开发者ID:therelaxist,项目名称:spring-usc,代码行数:22,代码来源:Interpretor.java

示例9: 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());
}
 
开发者ID:ghajba,项目名称:JaPy,代码行数:22,代码来源:PythonCalling.java

示例10: loadPythonScript

import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private <T> T loadPythonScript(String scriptPythonType, Class<T> scriptJavaType, PyObject[] constructorArgs, PythonInterpreter interpreter)
		throws PythonException {
	PyObject scriptPythonTypeObject = interpreter.get(scriptPythonType);
       if (scriptPythonTypeObject == null) {
       	return null;
       }
       

       PyObject scriptPythonTypeClass;
       try {
       	scriptPythonTypeClass = scriptPythonTypeObject.__call__(constructorArgs);
	} catch (Exception ex) {
		log.error("Failed to initialize python class", ex.getMessage());
		throw new PythonException(String.format("Failed to initialize python class '%s'", scriptPythonType), ex);
	}

       Object scriptJavaClass = scriptPythonTypeClass.__tojava__(scriptJavaType);
       if (!ReflectHelper.assignableFrom(scriptJavaClass.getClass(), scriptJavaType)) {
       	return null;

       }

       return (T) scriptJavaClass;
}
 
开发者ID:GluuFederation,项目名称:oxCore,代码行数:26,代码来源:PythonService.java

示例11: evalLookupLexer

import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
private Object evalLookupLexer(PyGateway gateway, String alias, Object notFoundFallback) {
    Object result;
    try {
        PythonInterpreter interpreter = gateway.getInterpreter();
        interpreter.set("alias", alias);
        interpreter.exec(""
                + "from pygments.lexers import get_lexer_by_name\n"
                + "result = get_lexer_by_name(alias)");

        result = interpreter.get("result");
    } catch (PyException e) {
        log.warn("Unable to find Pygments lexer for alias '{}'", alias);
        result = notFoundFallback;
    }
    return result;
}
 
开发者ID:Arnauld,项目名称:gutenberg,代码行数:17,代码来源:Lexers.java

示例12: loadFromFile

import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
 * Loads a module from a Python file that contains a class with the <b>SAME</b> name as the file.
 * The class must extend {@link ExternalModule}.
 *
 * @param f the file to load the {@link org.freecode.irc.votebot.api.ExternalModule} from
 * @return the {@link ExternalModule} loaded, or <tt>null</tt>
 * @throws IOException     if the file was not found
 * @throws ScriptException if the script failed to load
 */
public ExternalModule loadFromFile(final File f) throws IOException, ScriptException {
    if (f == null || !f.exists()) {
        throw new IOException("Invalid file");
    }
    if (f.getName().endsWith(".py")) {
        PythonInterpreter interpreter = new PythonInterpreter();
        String clzName = f.getName().replace(".py", "");
        interpreter.exec(String.format("from %s import %s", clzName, clzName));
        PyObject pyClass = interpreter.get(clzName);
        PyObject buildObject = pyClass.__call__();
        ExternalModule ext = (ExternalModule) buildObject.__tojava__(ExternalModule.class);
        ext.setFvb(fvb);
        ext.setExtName(clzName);
        interpreter.cleanup();
        return ext;
    }
    return null;

}
 
开发者ID:freecode,项目名称:FreeVoteBot,代码行数:29,代码来源:ScriptModuleLoader.java

示例13: main

import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public static void main(String[] args) {
    String directory = System.getProperty("user.home") + File.separator + ".fvb-modules";
    File dir = new File(directory);
    if (dir.exists()) {
        try {
            Properties props = new Properties();
            props.setProperty("python.path", directory);
            PythonInterpreter.initialize(System.getProperties(), props, new String[]{""});
            PythonInterpreter interpreter = new PythonInterpreter();
           // PyCode code = interpreter.compile(new FileReader(new File(directory, "TestModule.py")));
            interpreter.exec("from TestModule import TestModule");
            //interpreter.eval(code);
           // System.out.println(interpreter.getLocals());
            PyObject object = interpreter.get("TestModule");

            PyObject buildObject = object.__call__();
            ExternalModule ext = (ExternalModule) buildObject.__tojava__(ExternalModule.class);
            System.out.println(ext.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
开发者ID:freecode,项目名称:FreeVoteBot,代码行数:24,代码来源:JythonTest.java

示例14: checkImplements

import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
 * Check that a type implements a Java interface
 *
 * @param langType type with langauge
 * @param interfaceClass java interface class
 * @param interpreter PythonInterpreter
 */
private static void checkImplements(LanguageAndType langType,
    Class interfaceClass, PythonInterpreter interpreter) {
  switch (langType.getLanguage()) {
  case JAVA:
    checkArgument(interfaceClass.isAssignableFrom(langType.getJavaClass()),
        langType.getJavaClass().getSimpleName() + " needs to implement " +
        interfaceClass.getSimpleName());
    break;
  case JYTHON:
    PyObject pyClass = interpreter.get(langType.getJythonClassName());
    PyObject pyObj = pyClass.__call__();
    Object converted = pyObj.__tojava__(interfaceClass);
    checkArgument(!Py.NoConversion.equals(converted),
       "Jython class " + langType.getJythonClassName() +
       " does not implement " + interfaceClass.getSimpleName() +
       " interface");
    break;
  default:
    throw new IllegalArgumentException("Don't know how to handle " +
        "language " + langType.getLanguage());
  }
}
 
开发者ID:renato2099,项目名称:giraph-gora,代码行数:30,代码来源:HiveJythonUtils.java

示例15: loadJython

import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
public static synchronized Object loadJython(String tagName, String[] armNames, Element config)
throws Exception {
    Class mustImplement = load(tagName.substring("jython".length()), armNames);
    if (getElement(config, "inline") != null) {
        String jythonCode = getNestedText(getElement(config, "inline"));
        return inlineJython(tagName+pythonClassNum++, mustImplement, jythonCode);
    } else {
        String moduleName = getNestedText(SodUtil.getElement(config, "module"));
        String className = getNestedText(SodUtil.getElement(config, "class"));
        PythonInterpreter interp = getPythonInterpreter();
        interp.exec("from "+moduleName+" import "+className);
        PyObject jyWaveformProcessClass = interp.get(className);
        PyObject pyWaveformProcessObj = jyWaveformProcessClass.__call__( PyJavaType.wrapJavaObject(config));
        return mustImplement.cast(pyWaveformProcessObj.__tojava__(mustImplement));
    }
}
 
开发者ID:crotwell,项目名称:sod,代码行数:17,代码来源:SodUtil.java


注:本文中的org.python.util.PythonInterpreter.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。