Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r60103:b6a97a30f8f4
Date: 2013-01-15 12:30 -0800
http://bitbucket.org/pypy/pypy/changeset/b6a97a30f8f4/

Log:    itertools.accumulate

diff --git a/pypy/module/itertools/__init__.py 
b/pypy/module/itertools/__init__.py
--- a/pypy/module/itertools/__init__.py
+++ b/pypy/module/itertools/__init__.py
@@ -23,6 +23,7 @@
     """
 
     interpleveldefs = {
+        'accumulate'    : 'interp_itertools.W_Accumulate',
         'chain'         : 'interp_itertools.W_Chain',
         'combinations'  : 'interp_itertools.W_Combinations',
         'combinations_with_replacement' : 
'interp_itertools.W_CombinationsWithReplacement',
diff --git a/pypy/module/itertools/interp_itertools.py 
b/pypy/module/itertools/interp_itertools.py
--- a/pypy/module/itertools/interp_itertools.py
+++ b/pypy/module/itertools/interp_itertools.py
@@ -1185,3 +1185,38 @@
 
 permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)""",
 )
+
+
+class W_Accumulate(Wrappable):
+
+    def __init__(self, space, w_iterable):
+        self.space = space
+        self.w_iterable = w_iterable
+        self.w_total = None
+
+    def iter_w(self):
+        return self.space.wrap(self)
+
+    def next_w(self):
+        w_value = self.space.next(self.w_iterable)
+        if self.w_total is None:
+            self.w_total = w_value
+            return w_value
+
+        self.w_total = self.space.add(self.w_total, w_value)
+        return self.w_total
+
+def W_Accumulate__new__(space, w_subtype, w_iterable):
+    r = space.allocate_instance(W_Accumulate, w_subtype)
+    r.__init__(space, space.iter(w_iterable))
+    return space.wrap(r)
+
+W_Accumulate.typedef = TypeDef("accumulate",
+    __module__ = 'itertools',
+    __new__  = interp2app(W_Accumulate__new__),
+    __iter__ = interp2app(W_Accumulate.iter_w),
+    __next__ = interp2app(W_Accumulate.next_w),
+    __doc__  = """\
+"accumulate(iterable) --> accumulate object
+
+Return series of accumulated sums.""")
diff --git a/pypy/module/itertools/test/test_itertools.py 
b/pypy/module/itertools/test/test_itertools.py
--- a/pypy/module/itertools/test/test_itertools.py
+++ b/pypy/module/itertools/test/test_itertools.py
@@ -918,3 +918,18 @@
                 assert list(itertools.islice(c2, 3)) == expected
                 c3 = pickle.loads(pickle.dumps(c))
                 assert list(itertools.islice(c3, 3)) == expected
+
+class AppTestItertools32:
+    spaceconfig = dict(usemodules=['itertools'])
+
+    def setup_class(cls):
+        if cls.space.is_true(cls.space.appexec([], """():
+            import sys; return sys.version_info < (3, 2)
+            """)):
+            py.test.skip("Requires Python 3.2")
+
+    def test_accumulate(self):
+        from itertools import accumulate
+        expected = [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
+        assert list(accumulate(range(10))) == expected
+        assert list(accumulate(iterable=range(10))) == expected
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to