@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)); }
@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; }
static PyTuple makeIndexedTuple(PyTuple pool, int indices[], int end) { PyObject items[] = new PyObject[end]; for (int i = 0; i < end; i++) { items[i] = pool.__getitem__(indices[i]); } return new PyTuple(items); } }
public List subList(int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > size()) { throw new IndexOutOfBoundsException(); } else if (fromIndex > toIndex) { throw new IllegalArgumentException(); } PyObject elements[] = new PyObject[toIndex - fromIndex]; for (int i = 0, j = fromIndex; i < elements.length; i++, j++) { elements[i] = array[j]; } return new PyTuple(elements); }
@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; }
@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); }
private static void fill_classic_mro(List<PyObject> acc, PyClass classic_cl) { if (!acc.contains(classic_cl)) { acc.add(classic_cl); } PyObject[] bases = classic_cl.__bases__.getArray(); for (PyObject base : bases) { fill_classic_mro(acc,(PyClass)base); } }
state = new PyTuple(state, slots); PyObject newobj = copyreg.__findattr__("__newobj__"); n = ((PyTuple)args).size(); PyObject args2[] = new PyObject[n+1]; args2[0] = cls; for(i = 0; i < n; i++) { args2[i+1] = ((PyTuple)args).pyget(i); return new PyTuple(newobj, new PyTuple(args2), state, listitems, dictitems);
write_byte(TYPE_TUPLE); PyTuple t = (PyTuple) v; int n = t.__len__(); write_int(n); for (int i = 0; i < n; i++) { write_object(t.__getitem__(i), depth + 1); for (PyObject item : dict.iteritems().asIterable()) { PyTuple pair = (PyTuple) item; write_object(pair.__getitem__(0), depth + 1); write_object(pair.__getitem__(1), depth + 1); write_int(code.co_flags.toBits()); write_object(Py.newString(new String(code.co_code)), depth + 1); write_object(new PyTuple(code.co_consts), depth + 1); write_strings(code.co_names, depth + 1); write_strings(code.co_varnames, depth + 1);
public static PyObject filterTuple(PyObject func, PyTuple seq) { int len = seq.size(); if (len == 0) { if (seq.getType() != PyTuple.TYPE) { return Py.EmptyTuple; } return seq; } PyList list = new PyList(); PyObject item; for (int i = 0; i < len; i++) { item = seq.__finditem__(i); if (func == Py.None) { if (!item.__nonzero__()) { continue; } } else if (!func.__call__(item).__nonzero__()) { continue; } list.append(item); } return PyTuple.fromIterable(list); }
protected PyObject repeat(int count) { if (count < 0) { count = 0; } int size = size(); if (size == 0 || count == 1) { if (getType() == TYPE) { // Since tuples are immutable, we can return a shared copy in this case return this; } if (size == 0) { return EMPTY_TUPLE; } } int newSize = size * count; if (newSize / size != count) { throw Py.MemoryError(""); } PyObject[] newArray = new PyObject[newSize]; for (int i = 0; i < count; i++) { System.arraycopy(array, 0, newArray, i * size, size); } return fromArrayNoCopy(newArray); }
private static boolean abstractIsSubClass(PyObject derived, PyObject cls) { while (true) { if (derived == cls) { return true; } PyTuple bases = abstractGetBases(derived); if (bases == null) { return false; } int basesSize = bases.size(); if (basesSize == 0) { return false; } if (basesSize == 1) { // Avoid recursivity in the single inheritance case derived = bases.pyget(0); continue; } for (PyObject base : bases.asIterable()) { if (abstractIsSubClass(base, cls)) { return true; } } return false; } }
@ExposedMethod(doc = BuiltinDocs.tuple___len___doc) final int tuple___len__() { return size(); }
@ExposedMethod(defaults = {"null", "null"}, doc = BuiltinDocs.tuple_index_doc) final int tuple_index(PyObject value, PyObject start, PyObject stop) { int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start); int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop); return tuple_index(value, startInt, stopInt); }
private static int item(PyTuple tup, int i) { int val = tup.__getitem__(i).asInt(); boolean valid = true; switch (i) {
public PyObject __iternext__() { PyObject args = nextElement(iterator); PyObject result = null; if (args != null) { PyTuple argTuple = PyTuple.fromIterable(args); // convert to array of PyObjects in call to function result = callable.__call__(argTuple.getArray()); } return result; }
if (o instanceof PyTuple) { PyTuple tuple = (PyTuple) o; int len = tuple.__len__(); if ((globals == null || globals.equals(None)) && (locals == null || locals.equals(None)) && (len >= 2 && len <= 3)) { o = tuple.__getitem__(0); globals = tuple.__getitem__(1); if (len == 3) { locals = tuple.__getitem__(2);