private static synchronized PythonInterpreter initPythonInterpreter(String[] args, String pythonPath, String scriptName) { if (!jythonInitialized) { // the java stack traces within the jython runtime aren't useful for users System.getProperties().put("python.options.includeJavaStackInExceptions", "false"); PySystemState.initialize(System.getProperties(), new Properties(), args); pythonInterpreter = new PythonInterpreter(); pythonInterpreter.getSystemState().path.add(0, pythonPath); pythonInterpreter.setErr(System.err); pythonInterpreter.setOut(System.out); pythonInterpreter.exec("import " + scriptName); jythonInitialized = true; } return pythonInterpreter; } }
public synchronized static void initializePython() { if (isInitialized.get()) return; // Set the Jython package cache directory. Properties jythonProperties = new Properties(); String jythonCacheDir = Platform.getUserDataDirectory() + Platform.SEP + "_jythoncache"; jythonProperties.put("python.cachedir", jythonCacheDir); // Initialize Python. PySystemState.initialize(System.getProperties(), jythonProperties, new String[]{""}); // Add the built-in Python libraries. File nodeBoxLibraries = new File(libDir, "nodeboxlibs.zip"); Py.getSystemState().path.add(new PyString(nodeBoxLibraries.getAbsolutePath())); // This folder contains unarchived NodeBox libraries. // Only used in development. File developmentLibraries = new File("src/main/python"); Py.getSystemState().path.add(new PyString(developmentLibraries.getAbsolutePath())); // Add the user's Python directory. Py.getSystemState().path.add(new PyString(Platform.getUserPythonDirectory().getAbsolutePath())); isInitialized.set(true); }
public PyList(PyType type, Collection c) { super(type); list = new ArrayList<PyObject>(c.size()); for (Object o : c) { add(o); } }
private PyList(List list, boolean convert) { super(TYPE); if (!convert) { this.list = list; } else { this.list = Generic.list(); for (Object o : list) { add(o); } } }
@Override public PyObject __call__() { PyList keys = new PyList(); for (Object key : asMap().keySet()) { keys.add(Py.java2py(key)); } return keys; } };
@Override public PyObject __call__() { PyList values = new PyList(); for (Object value : asMap().values()) { values.add(Py.java2py(value)); } return values; } };
@Override public PyObject __call__() { PyList items = new PyList(); for (Map.Entry<Object, Object> entry : asMap().entrySet()) { items.add(new PyTuple(Py.java2py(entry.getKey()), Py.java2py(entry.getValue()))); } return items; } };
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.list___add___doc) final synchronized PyObject list___add__(PyObject o) { PyList sum = null; if (o instanceof PySequenceList && !(o instanceof PyTuple)) { if (o instanceof PyList) { List oList = ((PyList) o).list; List newList = new ArrayList(list.size() + oList.size()); newList.addAll(list); newList.addAll(oList); sum = fromList(newList); } } else if (!(o instanceof PySequenceList)) { // also support adding java lists (but not PyTuple!) Object oList = o.__tojava__(List.class); if (oList != Py.NoConversion && oList != null) { List otherList = (List) oList; sum = new PyList(); sum.list_extend(this); for (Iterator i = otherList.iterator(); i.hasNext();) { sum.add(i.next()); } } } return sum; }
public static PyObject reflectionTraverseTestPyList() { PyReflectionTraversed result = new PyReflectionTraversed(); result.referent1 = new PyList(); result.refArray[0] = new PyInteger(247); result.refArray[1] = new PyString("test4"); result.refArray[2] = new PyInteger(335); result.refList.add(new PyFloat(0.73)); result.refList.add(new PyFloat(-0.73)); PyList pl = new PyList(); result.referent2 = pl; pl.add(Py.True); pl.add(new PyString("test5")); pl.add(result); return result; }
private void setPythonPath(SpongeEngine engine) { if (engine != null) { String pythonPath = getEngineOperations().getProperty(PROP_PYTHON_PATH, null); if (pythonPath != null) { PySystemState engineSys = new PySystemState(); List<String> paths = Arrays.asList(StringUtils.split(pythonPath, PROP_PATH_SEPARATOR)); Collections.reverse(paths); for (String pathElement : paths) { engineSys.path.add(0, Py.newString(pathElement)); } Py.setSystemState(engineSys); } } }
PyList getAsList() { PyList list = new PyList(); PyTuple metric = new PyTuple(metricName, new PyTuple(timestamp, metricValue)); list.add(metric); return list; }
public void generate_dds_idl(String argFile, List<String> subFolders, List<String> extensions) { interp.set("argFile", new PyString(argFile)); PyList subFolderList = new PyList(); for(String subFolder : subFolders) subFolderList.add(new PyString(subFolder)); interp.set("subFolders", subFolderList); if(extensions != null && extensions.size() > 0) { PyList extensionList = new PyList(); for(String extension : extensions) extensionList.add(new PyString(extension)); interp.set("extension", extensionList); } else interp.set("extension", Py.None); interp.exec("generate_dds_idl(argFile, subFolders, extension)"); }
public static PyObject pigToPython(Object object) { if (object instanceof Tuple) { return pigTupleToPyTuple((Tuple) object); } else if (object instanceof DataBag) { PyList list = new PyList(); for (Tuple bagTuple : (DataBag) object) { list.add(pigTupleToPyTuple(bagTuple)); } return list; } else if (object instanceof Map<?, ?>) { PyDictionary newMap = new PyDictionary(); for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) { newMap.put(entry.getKey(), pigToPython(entry.getValue())); } return newMap; } else if (object instanceof DataByteArray) { return Py.java2py(((DataByteArray) object).get()); } else { return Py.java2py(object); } }
@ExposedMethod(doc = tolist_doc) final PyList memoryview_tolist() { checkNotReleased(); int n = backing.getLen(); PyList list = new PyList(); for (int i = 0; i < n; i++) { list.add(new PyInteger(backing.intAt(i))); } return list; }
@ExposedMethod(doc = readall_doc) final synchronized PyObject _RawIOBase_readall() { // Get reference to the (possibly overridden) read() method PyObject readMethod = __getattr__("read"); // Quite often, a single read operation will do the trick PyObject prev = readMethod.__call__(_io.DEFAULT_BUFFER_SIZE); if (!prev.__nonzero__()) { // Nothing on the first read: that means we're done return prev; } else { // Try a second read PyObject curr = readMethod.__call__(_io.DEFAULT_BUFFER_SIZE); if (!curr.__nonzero__()) { // Nothing on the second read: the result is just the first one return prev; } else { // Remembering more than one thing is hard: we're going to need a list PyList list = new PyList(); list.add(prev); // Accumulate the current read result and get another, until we run out of bytes. do { list.add(curr); curr = readMethod.__call__(_io.DEFAULT_BUFFER_SIZE); } while (curr.__nonzero__()); // Stitch it all together return Py.EmptyString.join(list); } } }
@ExposedMethod public PyList BZ2File_readlines(PyObject[] args, String[] kwds) { checkInIterMode(); checkReadBufferInit(); // make sure file data valid if (buffer == null || buffer.closed()) { throw Py.ValueError("Cannot call readlines() on a closed file"); } PyList lineList = new PyList(); PyString line = null; while (!(line = BZ2File_readline(args, kwds)).equals(Py.EmptyString)) { lineList.add(line); } return lineList; }
protected final PyObject importer_load_module(String fullname) { ModuleCodeData moduleCodeData = getModuleCode(fullname); if (moduleCodeData == null) { return Py.None; } // the module *must* be in sys.modules before the loader executes the module code; the // module code may (directly or indirectly) import itself PyModule mod = imp.addModule(fullname); mod.__dict__.__setitem__("__loader__", this); if (moduleCodeData.isPackage) { // add __path__ to the module *before* the code gets executed PyList pkgpath = new PyList(); pkgpath.add(makePackagePath(fullname)); mod.__dict__.__setitem__("__path__", pkgpath); } imp.createFromCode(fullname, moduleCodeData.code, moduleCodeData.path); Py.writeDebug("import", "import " + fullname + " # loaded from " + moduleCodeData.path); return mod; }
gc.garbage.add(toFinalize); if ((gc.getJythonGCFlags() & gc.VERBOSE_FINALIZE) != 0) { gc.writeDebug("gc", toFinalize+" added to garbage.");