org.python.core.PyObject Java Examples
The following examples show how to use
org.python.core.PyObject.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FrmMain.java From MeteoInfo with GNU Lesser General Public License v3.0 | 6 votes |
/** * Load an application * * @param plugin Application */ public void loadApplication(Application plugin) { this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); try { PythonInterpreter interp = this.getConsoleDockable().getInterpreter(); String path = plugin.getPath(); interp.exec("import " + path); interp.exec("from " + path + ".loadApp import LoadApp"); PyObject loadClass = interp.get("LoadApp"); PyObject loadObj = loadClass.__call__(); IPlugin instance = (IPlugin) loadObj.__tojava__(IPlugin.class); instance.setApplication(FrmMain.this); instance.setName(plugin.getName()); plugin.setPluginObject(instance); plugin.setLoad(true); instance.load(); } catch (Exception e) { e.printStackTrace(); } this.setCursor(Cursor.getDefaultCursor()); }
Example #2
Source File: PythonExecutor.java From score with Apache License 2.0 | 6 votes |
protected Serializable eval(String prepareEnvironmentScript, String script) { if (interpreter.get(TRUE) == null) { interpreter.set(TRUE, Boolean.TRUE); } if (interpreter.get(FALSE) == null) { interpreter.set(FALSE, Boolean.FALSE); } if(prepareEnvironmentScript != null && !prepareEnvironmentScript.isEmpty()) { interpreter.exec(prepareEnvironmentScript); } PyObject evalResultAsPyObject = interpreter.eval(script); Serializable evalResult; evalResult = resolveJythonObjectToJavaEval(evalResultAsPyObject, script); return evalResult; }
Example #3
Source File: JIntrospect.java From MeteoInfo with GNU Lesser General Public License v3.0 | 6 votes |
/** * Complete package name * * @param target Target * @return Package names */ public List<String> completePackageName(String target) { String[] targetComponents = target.split("\\."); String base = targetComponents[0]; PySystemState state = interp.getSystemState(); PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__")); PyObject module = importer.__call__(Py.newString(base)); if (targetComponents.length > 1) { for (int i = 1; i < targetComponents.length; i++) { module = module.__getattr__(targetComponents[i]); } } PyList plist = (PyList) module.__dir__(); List<String> list = new ArrayList<>(); String name; for (int i = 0; i < plist.__len__(); i++) { name = plist.get(i).toString(); if (!name.startsWith("__")) { list.add(name); } } //list.add("*"); return list; }
Example #4
Source File: JythonUtils.java From AndroidRobot with Apache License 2.0 | 6 votes |
public static List<Object> getList(ArgParser ap, int position) /* */ { /* 169 */ PyObject arg = ap.getPyObject(position, Py.None); /* 170 */ if (Py.isInstance(arg, PyNone.TYPE)) { /* 171 */ return Collections.emptyList(); /* */ } /* */ /* 174 */ List ret = Lists.newArrayList(); /* 175 */ PyList array = (PyList)arg; /* 176 */ for (int x = 0; x < array.__len__(); x++) { /* 177 */ PyObject item = array.__getitem__(x); /* */ /* 179 */ Class javaClass = (Class)PYOBJECT_TO_JAVA_OBJECT_MAP.get(item.getClass()); /* 180 */ if (javaClass != null) { /* 181 */ ret.add(item.__tojava__(javaClass)); /* */ } /* */ } /* 184 */ return ret; /* */ }
Example #5
Source File: BlockMethods.java From pycode-minecraft with MIT License | 5 votes |
public void circle(PyObject[] args, String[] kws) { if (this.world == null || this.world.isRemote) return; ArgParser r = new ArgParser("line", s("pos", "radius", "blockname"), // TODO PyRegistry.BLOCK_VARIATIONS s("color", "facing", "type", "half", "shape", "fill")); r.parse(args, kws); MyBlockPos mpos = (MyBlockPos)r.get("pos").__tojava__(MyBlockPos.class); ShapeGen.circle(r, this.world, mpos.blockPos, EnumFacing.NORTH); }
Example #6
Source File: HandMethods.java From pycode-minecraft with MIT License | 5 votes |
public void alter(PyObject[] args, String[] kws) { if (this.world == null || this.world.isRemote) return; ArgParser r = new ArgParser("put", s(), PyRegistry.BLOCK_VARIATIONS); r.parse(args, kws); BlockPos pos = this.hand.getFacedPos(); IBlockState state = this.world.getBlockState(pos); EnumFacing facing = PyRegistry.getBlockFacing(state); IBlockState modified = PyRegistry.modifyBlockStateFromSpec(state, r, facing); if (state != modified) { this.world.setBlockState(pos, modified); } }
Example #7
Source File: PythonSinkFunction.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override public void invoke(PyObject value, Context context) throws Exception { try { this.fun.invoke(value, context); } catch (PyException pe) { throw createAndLogException(pe); } }
Example #8
Source File: JythonEngine.java From commons-bsf with Apache License 2.0 | 5 votes |
public void runcode(PyObject code) { try { this.exec(code); } catch (PyException exc) { throw exc; } }
Example #9
Source File: PythonMapFunction.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override public PyObject map(PyObject value) throws Exception { try { return AdapterMap.adapt(fun.map(value)); } catch (PyException pe) { throw createAndLogException(pe); } }
Example #10
Source File: PythonApplyFunction.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override public void apply(PyKey key, W window, Iterable<PyObject> values, Collector<PyObject> out) throws Exception { this.collector.setCollector(out); try { this.fun.apply(key.getData(), window, values, this.collector); } catch (PyException pe) { throw createAndLogException(pe); } }
Example #11
Source File: HandMethods.java From pycode-minecraft with MIT License | 5 votes |
public void floor(PyObject[] args, String[] kws) { if (this.world == null || this.world.isRemote) return; ArgParser r = new ArgParser("line", s("width", "depth", "blockname"), PyRegistry.BLOCK_VARIATIONS); r.parse(args, kws); ShapeGen.floor(r, this.world, this.hand.getFacedPos(), this.hand.getHorizontalFacing()); }
Example #12
Source File: BlockMethods.java From pycode-minecraft with MIT License | 5 votes |
public void roof(PyObject[] args, String[] kws) throws BlockTypeError { if (this.world == null || this.world.isRemote) return; ArgParser r = new ArgParser("roof", s("pos", "width", "depth", "blockname"), // TODO PyRegistry.BLOCK_VARIATIONS s("style", "color", "facing", "type", "half", "shape", "fill")); r.parse(args, kws); MyBlockPos mpos = (MyBlockPos)r.get("pos").__tojava__(MyBlockPos.class); RoofGen.roof(r, this.world, mpos.blockPos, EnumFacing.NORTH); }
Example #13
Source File: BlockMethods.java From pycode-minecraft with MIT License | 5 votes |
public void alter(PyObject[] args, String[] kws) { if (this.world == null || this.world.isRemote) return; ArgParser r = new ArgParser("put", s("pos"), PyRegistry.BLOCK_VARIATIONS); r.parse(args, kws); MyBlockPos mpos = (MyBlockPos)r.get("pos").__tojava__(MyBlockPos.class); BlockPos pos = mpos.blockPos; IBlockState state = this.world.getBlockState(pos); EnumFacing facing = PyRegistry.getBlockFacing(state); IBlockState modified = PyRegistry.modifyBlockStateFromSpec(state, r, facing); if (state != modified) { this.world.setBlockState(pos, modified); } }
Example #14
Source File: JythonUtils.java From AndroidRobot with Apache License 2.0 | 5 votes |
public static double getFloat(ArgParser ap, int position) /* */ { /* 129 */ PyObject arg = ap.getPyObject(position); /* */ /* 131 */ if (Py.isInstance(arg, PyFloat.TYPE)) { /* 132 */ return ((PyFloat)arg).asDouble(); /* */ } /* 134 */ if (Py.isInstance(arg, PyInteger.TYPE)) { /* 135 */ return ((PyInteger)arg).asDouble(); /* */ } /* 137 */ throw Py.TypeError("Unable to parse argument: " + position); /* */ }
Example #15
Source File: PythonCode.java From pycode-minecraft with MIT License | 5 votes |
public void invoke(String method) { PyObject obj = (PyObject) this.bindings.get(method); if (obj == null) { failz0r(world, pos, "Unknown function '%s'", method); return; } PyFunction func = (PyFunction)obj; try { func.__call__(); } catch (RuntimeException e) { failz0r(world, pos, "Error running code: %s", e.toString()); } }
Example #16
Source File: BlockMethods.java From pycode-minecraft with MIT License | 5 votes |
public void line(PyObject[] args, String[] kws) { ArgParser r = new ArgParser("line", s("pos", "distance", "blockname"), PyRegistry.BLOCK_VARIATIONS); r.parse(args, kws); MyBlockPos mpos = (MyBlockPos)r.get("pos").__tojava__(MyBlockPos.class); ShapeGen.line(r, this.world, mpos.blockPos, EnumFacing.NORTH); }
Example #17
Source File: ArgParser.java From pycode-minecraft with MIT License | 5 votes |
private int asInt(PyObject value) { if(value instanceof PyFloat) { Py.warning(Py.DeprecationWarning, "integer argument expected, got float"); value = value.__int__(); } return value.asInt(); }
Example #18
Source File: JythonFunction.java From spork with Apache License 2.0 | 5 votes |
@Override public Schema outputSchema(Schema input) { if(schema != null) { return schema; } else { if(outputSchemaFunc != null) { PyFunction pf; try { pf = JythonScriptEngine.getFunction(scriptFilePath, outputSchemaFunc); // this should be a schema function PyObject schemaFunctionDef = pf.__findattr__("schemaFunction".intern()); if(schemaFunctionDef == null) { throw new IllegalStateException("Function: " + outputSchemaFunc + " is not a schema function"); } Object newSchema = ((pf.__call__(Py.java2py(input))).__tojava__(Object.class)); if (newSchema instanceof ResourceSchema) { return(Schema.getPigSchema((ResourceSchema) newSchema)); } else if (newSchema instanceof Schema) { return (Schema) newSchema; } else { return Utils.getSchemaFromString(newSchema.toString()); } } catch (IOException ioe) { throw new IllegalStateException("Could not find function: " + outputSchemaFunc + "()", ioe); } } else { return new Schema(new Schema.FieldSchema(null, DataType.BYTEARRAY)); } } }
Example #19
Source File: PythonReduceFunction.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override public PyObject reduce(PyObject value1, PyObject value2) throws Exception { if (fun == null) { fun = SerializationUtils.deserializeObject(serFun); } try { return AdapterMap.adapt(this.fun.reduce(value1, value2)); } catch (PyException pe) { throw AbstractPythonUDF.createAndLogException(pe, LOG); } }
Example #20
Source File: PythonOutputSelector.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override public Iterable<String> select(PyObject value) { if (this.fun == null) { try { fun = SerializationUtils.deserializeObject(serFun); } catch (Exception e) { throw new FlinkRuntimeException("Failed to deserialize user-defined function.", e); } } try { return this.fun.select(value); } catch (PyException pe) { throw new FlinkRuntimeException(AbstractPythonUDF.createAndLogException(pe, LOG)); } }
Example #21
Source File: JythonUtils.java From spork with Apache License 2.0 | 5 votes |
public static PyTuple pigTupleToPyTuple(Tuple tuple) { PyObject[] pyTuple = new PyObject[tuple.size()]; int i = 0; for (Object object : tuple.getAll()) { pyTuple[i++] = pigToPython(object); } return new PyTuple(pyTuple); }
Example #22
Source File: PythonFilterFunction.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override public boolean filter(PyObject value) throws Exception { try { return this.fun.filter(value); } catch (PyException pe) { throw createAndLogException(pe); } }
Example #23
Source File: PyObjectSerializer.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
public void write(Kryo kryo, Output output, PyObject po) { try { byte[] serPo = SerializationUtils.serializeObject(po); output.writeInt(serPo.length); output.write(serPo); } catch (IOException e) { throw new KryoException("Failed to serialize object.", e); } }
Example #24
Source File: HandMethods.java From pycode-minecraft with MIT License | 5 votes |
public void put(PyObject[] args, String[] kws) { if (this.world == null || this.world.isRemote) return; ArgParser r = new ArgParser("put", s("blockname"), PyRegistry.BLOCK_VARIATIONS); r.parse(args, kws); IBlockState state = PyRegistry.getBlockVariant(r, this.hand.getFacedPos(), this.hand.getHorizontalFacing(), (WorldServer)this.world); this.put(this.hand.getFacedPos(),state, this.hand.getHorizontalFacing()); }
Example #25
Source File: AdapterMap.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * Convert java object to its corresponding PyObject representation. * * @param o Java object * @return PyObject */ public static PyObject adapt(Object o) { if (o instanceof PyObject) { return (PyObject) o; } return Py.java2py(o); }
Example #26
Source File: JIntrospect.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
/** * Get auto complete list * * @param command The command * @param includeMagic * @param includeSingle * @param includeDouble * @return Auto complete list * @throws java.io.IOException */ public List<String> getAutoCompleteList(String command, boolean includeMagic, boolean includeSingle, boolean includeDouble) throws IOException { // Temp KLUDGE here rather than in console.py //command += "."; if (command.startsWith("import ") || command.startsWith("from ")) { String target = getPackageName(command); if (target == null) { return null; } return completePackageName(target); } String root = this.getRoot(command, "."); if (root.isEmpty()) return null; try { PyObject object = this.interp.eval(root); PyList plist = (PyList) object.__dir__(); List<String> list = new ArrayList<>(); String name; for (int i = 0; i < plist.__len__(); i++) { name = plist.get(i).toString(); if (!name.startsWith("__")) { list.add(name); } } return list; } catch (Exception e){ return null; } }
Example #27
Source File: BlockMethods.java From pycode-minecraft with MIT License | 5 votes |
public void cube(PyObject[] args, String[] kws) { if (this.world == null || this.world.isRemote) return; ArgParser r = new ArgParser("line", s("pos", "width", "depth", "height", "blockname"), PyRegistry.BLOCK_VARIATIONS); r.parse(args, kws); MyBlockPos mpos = (MyBlockPos)r.get("pos").__tojava__(MyBlockPos.class); ShapeGen.cube(r, this.world, mpos.blockPos, EnumFacing.NORTH); }
Example #28
Source File: JythonEngine.java From commons-bsf with Apache License 2.0 | 5 votes |
/** * call the named method of the given object. */ public Object call (Object object, String method, Object[] args) throws BSFException { try { PyObject[] pyargs = Py.EmptyObjects; if (args != null) { pyargs = new PyObject[args.length]; for (int i = 0; i < pyargs.length; i++) pyargs[i] = Py.java2py(args[i]); } if (object != null) { PyObject o = Py.java2py(object); return unwrap(o.invoke(method, pyargs)); } PyObject m = interp.get(method); if (m == null) m = interp.eval(method); if (m != null) { return unwrap(m.__call__(pyargs)); } return null; } catch (PyException e) { throw new BSFException (BSFException.REASON_EXECUTION_ERROR, "exception from Jython:\n" + e, e); } }
Example #29
Source File: FrmAppsManager.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
public Application readPyApp(String path, String fileName) { try { Application plugin = new Application(); plugin.setPath(path); plugin.setClassName("LoadApp"); PythonInterpreter interp = this.parent.getConsoleDockable().getInterpreter(); interp.exec("import " + path); interp.exec("from " + path + ".loadApp import LoadApp"); PyObject loadClass = interp.get("LoadApp"); PyObject loadObj = loadClass.__call__(); IPlugin instance = (IPlugin) loadObj.__tojava__(IPlugin.class); plugin.setPluginObject(instance); return plugin; } catch (Exception e) { e.printStackTrace(); } return null; }
Example #30
Source File: PythonCodeCompletionFactory.java From ghidra with Apache License 2.0 | 5 votes |
/** * Creates a new CodeCompletion from the given Python objects. * * @param description description of the new CodeCompletion * @param insertion what will be inserted to make the code complete * @param pyObj a Python Object * @return A new CodeCompletion from the given Python objects. */ public static CodeCompletion newCodeCompletion(String description, String insertion, PyObject pyObj) { JComponent comp = null; if (pyObj != null) { if (includeTypes) { /* append the class name to the end of the description */ String className = getSimpleName(pyObj.getClass()); if (pyObj instanceof PyInstance) { /* get the real class */ className = getSimpleName(((PyInstance) pyObj).instclass.__name__); } else if (className.startsWith("Py")) { /* strip off the "Py" */ className = className.substring("Py".length()); } description = description + " (" + className + ")"; } comp = new GDLabel(description); Iterator<Class<?>> iter = classes.iterator(); while (iter.hasNext()) { Class<?> testClass = iter.next(); if (testClass.isInstance(pyObj)) { comp.setForeground(classToColorMap.get(testClass)); break; } } } return new CodeCompletion(description, insertion, comp); }