Skip to content

Commit 58a5f98

Browse files
committed
Extract ifilter.
1 parent d0161b3 commit 58a5f98

3 files changed

Lines changed: 75 additions & 72 deletions

File tree

src/org/python/modules/itertools/chain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static final PyObject from_iterable(PyObject iterable) {
5555
@ExposedNew
5656
@ExposedMethod
5757
final void chain___init__(final PyObject[] args, String[] kwds) {
58-
ArgParser ap = new ArgParser("__init__", args, kwds, new String[] {"iterables"});
58+
ArgParser ap = new ArgParser("chain", args, kwds, new String[] {"iterables"});
5959

6060
//ArgParser always returns a PyTuple - I wonder why we make it pass back a PyObject?
6161
PyTuple tuple = (PyTuple)ap.getList(0);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Copyright (c) Jython Developers */
2+
package org.python.modules.itertools;
3+
4+
import org.python.core.ArgParser;
5+
import org.python.core.Py;
6+
import org.python.core.PyIterator;
7+
import org.python.core.PyObject;
8+
import org.python.core.PyString;
9+
import org.python.core.PyTuple;
10+
import org.python.core.PyType;
11+
import org.python.expose.ExposedClassMethod;
12+
import org.python.expose.ExposedGet;
13+
import org.python.expose.ExposedNew;
14+
import org.python.expose.ExposedMethod;
15+
import org.python.expose.ExposedType;
16+
17+
import java.util.ArrayList;
18+
19+
@ExposedType(name = "itertools.ifilter", base = PyObject.class)
20+
public class ifilter extends PyObject {
21+
22+
public static final PyType TYPE = PyType.fromClass(ifilter.class);
23+
private PyIterator iter;
24+
25+
public ifilter() {
26+
super();
27+
}
28+
29+
public ifilter(PyType subType) {
30+
super(subType);
31+
}
32+
33+
public ifilter(PyObject predicate, PyObject iterable) {
34+
super();
35+
ifilter___init__(predicate, iterable);
36+
}
37+
38+
@ExposedGet
39+
public static PyString __doc__ = new PyString(
40+
"ifilter(function or None, sequence) --> ifilter object\n\n"
41+
+ "Return those items of sequence for which function(item) is true.\nIf function is None, "
42+
+ "return the items that are true.");
43+
44+
/**
45+
* Creates an iterator that returns the items of the iterable for which
46+
* <code>predicate(item)</code> is <code>true</code>. If <code>predicate</code> is null
47+
* (None) return the items that are true.
48+
*/
49+
@ExposedNew
50+
@ExposedMethod
51+
final void ifilter___init__(PyObject[] args, String[] kwds) {
52+
ArgParser ap = new ArgParser("ifilter", args, kwds, "predicate", "iterable");
53+
ap.noKeywords();
54+
PyObject predicate = ap.getPyObject(0);
55+
PyObject iterable = ap.getPyObject(1);
56+
ifilter___init__(predicate, iterable);
57+
}
58+
59+
private void ifilter___init__(PyObject predicate, PyObject iterable) {
60+
iter = new itertools.FilterIterator(predicate, iterable, true);
61+
}
62+
63+
@ExposedMethod
64+
public PyObject __iter__() {
65+
return iter;
66+
}
67+
68+
@ExposedMethod
69+
public PyObject next() {
70+
return iter.next();
71+
}
72+
}

src/org/python/modules/itertools/itertools.java

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public static void classDictInit(PyObject dict) {
8484
dict.__setitem__("__doc__", __doc__);
8585
dict.__setitem__("chain", chain.TYPE);
8686
dict.__setitem__("imap", imap.TYPE);
87+
dict.__setitem__("ifilter", ifilter.TYPE);
88+
dict.__setitem__("izip", izip.TYPE);
8789

8890
// Hide from Python
8991
dict.__setitem__("classDictInit", null);
@@ -365,20 +367,6 @@ public PyObject __iternext__() {
365367
}
366368
}
367369

368-
public static PyString __doc__ifilter = new PyString(
369-
"ifilter(function or None, sequence) --> ifilter object\n\n"
370-
+ "Return those items of sequence for which function(item) is true.\nIf function is None, "
371-
+ "return the items that are true.");
372-
373-
/**
374-
* Creates an iterator that returns the items of the iterable for which
375-
* <code>predicate(item)</code> is <code>true</code>. If <code>predicate</code> is null
376-
* (None) return the items that are true.
377-
*/
378-
public static PyIterator ifilter(PyObject predicate, PyObject iterable) {
379-
return new FilterIterator(predicate, iterable, true);
380-
}
381-
382370
public static PyString __doc__ifilterfalse = new PyString(
383371
"'ifilterfalse(function or None, sequence) --> ifilterfalse object\n\n"
384372
+ "Return those items of sequence for which function(item) is false.\nIf function is None, "
@@ -393,63 +381,6 @@ public static PyIterator ifilterfalse(PyObject predicate, PyObject iterable) {
393381
return new FilterIterator(predicate, iterable, false);
394382
}
395383

396-
public static PyString __doc__izip = new PyString(
397-
"izip(iter1 [,iter2 [...]]) --> izip object\n\nReturn an izip object "
398-
+ "whose .next() method returns a tuple where\nthe i-th element comes from the i-th iterable argument. "
399-
+ "The .next()\nmethod continues until the shortest iterable in the argument sequence\nis exhausted and then it "
400-
+ "raises StopIteration. Works like the zip()\nfunction but consumes less memory by returning an iterator "
401-
+ "instead of\na list.");
402-
403-
/**
404-
* Create an iterator whose <code>next()</code> method returns a tuple where the i-th element
405-
* comes from the i-th iterable argument. Continues until the shortest iterable is exhausted.
406-
* (Code in this method is based on __builtin__.zip()).
407-
*
408-
*/
409-
public static PyIterator izip(PyObject[] argstar) {
410-
final int itemsize = argstar.length;
411-
412-
if (itemsize == 0) {
413-
return (PyIterator)(new PyXRange(0).__iter__());
414-
}
415-
416-
// Type check the arguments; they must be sequences.
417-
final PyObject[] iters = new PyObject[itemsize];
418-
419-
for (int i = 0; i < itemsize; i++) {
420-
PyObject iter = argstar[i].__iter__();
421-
if (iter == null) {
422-
throw Py.TypeError("izip argument #" + (i + 1)
423-
+ " must support iteration");
424-
}
425-
iters[i] = iter;
426-
}
427-
428-
return new ItertoolsIterator() {
429-
430-
public PyObject __iternext__() {
431-
if (itemsize == 0)
432-
return null;
433-
434-
PyObject[] next = new PyObject[itemsize];
435-
PyObject item;
436-
437-
for (int i = 0; i < itemsize; i++) {
438-
439-
item = nextElement(iters[i]);
440-
441-
if (item == null) {
442-
return null;
443-
}
444-
next[i] = item;
445-
}
446-
return new PyTuple(next);
447-
}
448-
449-
};
450-
451-
}
452-
453384
public static PyString __doc__starmap = new PyString(
454385
"starmap(function, sequence) --> starmap object\n\nReturn an "
455386
+ "iterator whose values are returned from the function evaluated\nwith an argument tuple taken from the "

0 commit comments

Comments
 (0)