@ExposedSet(name = "args") public void setArgs(PyObject val) { args = PyTuple.fromIterable(val); }
private void combinationsWithReplacement___init__(PyObject iterable, final int r) { final PyTuple pool = PyTuple.fromIterable(iterable); final int n = pool.__len__(); final int indices[] = new int[r]; for (int i = 0; i < r; i++) { indices[i] = 0; } iter = new itertools.ItertoolsIterator() { boolean firstthru = true; @Override public PyObject __iternext__() { if (firstthru) { firstthru = false; if (n == 0 && r > 0) { return null; } return itertools.makeIndexedTuple(pool, indices); } int i; for (i = r - 1 ; i >= 0 && indices[i] == n - 1; i--); if (i < 0) return null; indices[i]++; for (int j = i + 1; j < r; j++) { indices[j] = indices[j-1]; } return itertools.makeIndexedTuple(pool, indices); } }; }
private void permutations___init__(final PyObject iterable, final int r) { final PyTuple pool = PyTuple.fromIterable(iterable); final int n = pool.__len__(); final int indices[] = new int[n];
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; }
private void combinations___init__(PyObject iterable, final int r) { if (r < 0) throw Py.ValueError("r must be non-negative"); final PyTuple pool = PyTuple.fromIterable(iterable); final int n = pool.__len__(); final int indices[] = new int[r]; for (int i = 0; i < r; i++) { indices[i] = i; } iter = new itertools.ItertoolsIterator() { boolean firstthru = true; @Override public PyObject __iternext__() { if (r > n) { return null; } if (firstthru) { firstthru = false; return itertools.makeIndexedTuple(pool, indices); } int i; for (i = r-1; i >= 0 && indices[i] == i+n-r ; i--); if (i < 0) return null; indices[i]++; for (int j = i+1; j < r; j++) { indices[j] = indices[j-1] + 1; } return itertools.makeIndexedTuple(pool, indices); } }; }
static PyTuple unpack(FormatDef[] f, int size, String format, ByteStream str) { PyList res = new PyList(); int flen = format.length(); for (int j = 0; j < flen; j++) { char c = format.charAt(j); if (j == 0 && (c=='@' || c=='<' || c=='>' || c=='=' || c=='!')) continue; if (Character.isWhitespace(c)) continue; int num = 1; if (Character.isDigit(c)) { num = Character.digit(c, 10); while (++j < flen && Character.isDigit((c = format.charAt(j)))) num = num*10 + Character.digit(c, 10); if (j > flen) break; } FormatDef e = getentry(c, f); str.skip(align(str.size(), e) - str.size()); e.doUnpack(str, num, res); } return PyTuple.fromIterable(res); }
public static PyString asctime(PyObject obj) { PyTuple tup; if (obj instanceof PyTuple) { tup = (PyTuple)obj; } else { tup = PyTuple.fromIterable(obj); } int len = tup.__len__(); if (len != 9) { throw Py.TypeError( String.format("argument must be sequence of length 9, not %d", len)); } StringBuilder buf = new StringBuilder(25); buf.append(enshortdays[item(tup, 6)]).append(' '); buf.append(enshortmonths[item(tup, 1)]).append(' '); int dayOfMonth = item(tup, 2); if(dayOfMonth < 10){ buf.append(' '); } buf.append(dayOfMonth).append(' '); buf.append(_twodigit(item(tup, 3))).append(':'); buf.append(_twodigit(item(tup, 4))).append(':'); buf.append(_twodigit(item(tup, 5))).append(' '); return new PyString(buf.append(item(tup, 0)).toString()); }
@ExposedNew @ExposedMethod final void product___init__(PyObject[] args, String[] kws) { final int repeat; final int num_iterables; if (kws.length == 1 && kws[0] == "repeat") { repeat = args[args.length -1].asInt(); if (repeat < 0) { throw Py.ValueError("repeat argument cannot be negative"); } num_iterables = args.length - 1; } else { repeat = 1; num_iterables = args.length; } final PyTuple tuples[] = new PyTuple[num_iterables]; for (int i = 0; i < num_iterables; i++) { tuples[i] = PyTuple.fromIterable(args[i]); } product___init__(tuples, repeat); }
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); }
ret.append(PyTuple.fromIterable(results)); return ret;