Skip to content

Commit 852e415

Browse files
committed
Break itertools.cycle into a separate class. Thanks Jez Ng!
1 parent d7ae9a8 commit 852e415

6 files changed

Lines changed: 1229 additions & 42 deletions

File tree

CoreExposed.includes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ org/python/modules/_fileio/PyFileIO.class
6464
org/python/modules/_functools/PyPartial.class
6565
org/python/modules/_hashlib$Hash.class
6666
org/python/modules/itertools/chain.class
67+
org/python/modules/itertools/cycle.class
6768
org/python/modules/itertools/count.class
6869
org/python/modules/itertools/ifilterfalse.class
6970
org/python/modules/itertools/ifilter.class
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.python.modules.itertools;
2+
3+
import org.python.core.ArgParser;
4+
import org.python.core.PyIterator;
5+
import org.python.core.PyObject;
6+
import org.python.core.PyString;
7+
import org.python.core.PyType;
8+
import org.python.expose.ExposedGet;
9+
import org.python.expose.ExposedMethod;
10+
import org.python.expose.ExposedNew;
11+
import org.python.expose.ExposedType;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
@ExposedType(name = "itertools.count", base = PyObject.class)
17+
public class cycle extends PyObject {
18+
19+
public static final PyType TYPE = PyType.fromClass(cycle.class);
20+
private PyIterator iter;
21+
22+
@ExposedGet
23+
public static PyString __doc__ = new PyString(
24+
"cycle(iterable) --> cycle object\n\n" +
25+
"Return elements from the iterable until it is exhausted.\n" +
26+
"Then repeat the sequence indefinitely.");
27+
28+
public cycle() {
29+
super();
30+
}
31+
32+
public cycle(PyType subType) {
33+
super(subType);
34+
}
35+
36+
/**
37+
* Creates an iterator that iterates over an iterable, saving the values for each iteration.
38+
* When the iterable is exhausted continues to iterate over the saved values indefinitely.
39+
*/
40+
public cycle(PyObject sequence) {
41+
super();
42+
cycle___init__(sequence);
43+
}
44+
45+
@ExposedNew
46+
@ExposedMethod
47+
final void cycle___init__(final PyObject[] args, String[] kwds) {
48+
ArgParser ap = new ArgParser("cycle", args, kwds, new String[] {"iterable"}, 1);
49+
ap.noKeywords();
50+
cycle___init__(ap.getPyObject(0));
51+
}
52+
53+
private void cycle___init__(final PyObject sequence) {
54+
iter = new itertools.ItertoolsIterator() {
55+
List<PyObject> saved = new ArrayList<PyObject>();
56+
int counter = 0;
57+
PyObject iterator = sequence.__iter__();
58+
59+
boolean save = true;
60+
61+
public PyObject __iternext__() {
62+
if (save) {
63+
PyObject obj = nextElement(iterator);
64+
if (obj != null) {
65+
saved.add(obj);
66+
return obj;
67+
} else {
68+
save = false;
69+
}
70+
}
71+
if (saved.size() == 0) {
72+
return null;
73+
}
74+
75+
// pick element from saved List
76+
if (counter >= saved.size()) {
77+
// start over again
78+
counter = 0;
79+
}
80+
return saved.get(counter++);
81+
}
82+
83+
};
84+
}
85+
86+
@ExposedMethod
87+
public PyObject __iter__() {
88+
return iter;
89+
}
90+
91+
@ExposedMethod
92+
public PyObject next() {
93+
return iter.next();
94+
}
95+
96+
}
97+

0 commit comments

Comments
 (0)