@Test public void test3() { ScriptSource source = new ResourceScriptSource( new ClassPathResource("/org/springframework/integration/scripting/jsr223/test3.py")); Object obj = executor.executeScript(source); PyTuple tuple = (PyTuple) obj; assertEquals(1, tuple.get(0)); }
@Test public void test3WithVariables() { ScriptSource source = new ResourceScriptSource( new ClassPathResource("/org/springframework/integration/scripting/jsr223/test3.py")); HashMap<String, Object> variables = new HashMap<String, Object>(); variables.put("foo", "bar"); Object obj = executor.executeScript(source, variables); PyTuple tuple = (PyTuple) obj; assertNotNull(tuple); assertEquals(1, tuple.get(0)); }
@Override protected Map<String, Object> getParamsFromVariables() throws IOException { PyFrame frame = Py.getFrame(); @SuppressWarnings("unchecked") List<PyTuple> locals = ((PyStringMap) frame.getLocals()).items(); Map<String, Object> vars = new HashMap<String, Object>(); for (PyTuple item : locals) { String key = (String) item.get(0); Object obj = item.get(1); if (obj != null) { String value = item.get(1).toString(); vars.put(key, value); } } return vars; }
public void incrementMapForLoop(String keyName, String valueName, Context flowContext, ForLoopCondition forLoopCondition) { Validate.notNull(keyName, "loop key name cannot be null"); Validate.notNull(keyName, "loop value name cannot be null"); Validate.notNull(flowContext, "flow context cannot be null"); Validate.notNull(forLoopCondition, "for condition cannot be null"); PyTuple keyValueTuple = (PyTuple) forLoopCondition.next(); Serializable keyFromIteration = (Serializable) keyValueTuple.get(0); Serializable valueFromIteration = (Serializable) keyValueTuple.get(1); flowContext.putVariable(keyName, keyFromIteration); flowContext.putVariable(valueName, valueFromIteration); logger.debug("key name: " + keyName + ", value: " + keyFromIteration); logger.debug("value name: " + keyName + ", value: " + valueFromIteration); }
@ExposedMethod(doc = BuiltinDocs.set___contains___doc) final boolean dict_items___contains__(PyObject item) { if (item instanceof PyTuple) { PyTuple tupleItem = (PyTuple)item; if (tupleItem.size() == 2) { SimpleEntry entry = new SimpleEntry(tupleItem.get(0), tupleItem.get(1)); return dvDict.entrySet().contains(entry); } } return false; }
public static Object start_response(PyObject[] objs, String[] values) { PyString status = (PyString) objs[0]; int space = status.toString().indexOf(' '); WSGIResponse r = RESPONSE.get(); if (space != -1) { r.code = status.toString().substring(0, space); r.message = status.toString().substring(space + 1); } else { r.code = status.toString(); } if (objs.length > 1) { PyList headers = (PyList) objs[1]; for (Iterator i = headers.iterator(); i.hasNext(); ) { PyTuple tup = (PyTuple) i.next(); r.headers.put(tup.get(0).toString(), tup.get(1).toString()); } } return null; }
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; } }
@ExposedMethod(doc = BuiltinDocs.float_as_integer_ratio_doc) public PyTuple as_integer_ratio() { if (Double.isInfinite(value)) { throw Py.OverflowError("Cannot pass infinity to float.as_integer_ratio."); } if (Double.isNaN(value)) { throw Py.ValueError("Cannot pass NaN to float.as_integer_ratio."); } PyTuple frexp = math.frexp(value); double float_part = ((Double)frexp.get(0)).doubleValue(); int exponent = ((Integer)frexp.get(1)).intValue(); for (int i = 0; i < 300 && float_part != Math.floor(float_part); i++) { float_part *= 2.0; exponent--; } /* * self == float_part * 2**exponent exactly and float_part is integral. If FLT_RADIX != 2, * the 300 steps may leave a tiny fractional part to be truncated by PyLong_FromDouble(). */ PyLong numerator = new PyLong(float_part); PyLong denominator = new PyLong(1); PyLong py_exponent = new PyLong(Math.abs(exponent)); py_exponent = (PyLong)denominator.__lshift__(py_exponent); if (exponent > 0) { numerator = new PyLong(numerator.getValue().multiply(py_exponent.getValue())); } else { denominator = py_exponent; } return new PyTuple(numerator, denominator); }
@Override public Map<String, Parameter<?>> getOutputs(ScriptEngine engine) throws ScriptException { PyDictionary outputs = (PyDictionary) process(engine).__getattr__("outputs"); Map<String, Parameter<?>> map = new TreeMap<String, Parameter<?>>(); for (String name : (List<String>) outputs.keys()) { PyTuple output = (PyTuple) outputs.get(name); Object type = output.__getitem__(0); Object desc = output.__getitem__(1); // map every key in the optional dictionary as parameter metadata entry Map<String, Object> metadata = null; if (output.size() == 3) { PyDictionary meta = (PyDictionary) output.get(2); if (meta != null && !meta.isEmpty()) { metadata = new HashMap<String, Object>(); for (Object key : meta.keySet()) { metadata.put((String) key, meta.get(key)); } } } map.put(name, parameter(name, type, 1, 1, desc, null, metadata)); } return map; }