# [Numpy-svn] r5325 - branches/1.1.x/numpy/ma

numpy-svn@scip... numpy-svn@scip...
Mon Jun 30 02:12:00 CDT 2008

```Author: pierregm
Date: 2008-06-30 02:11:53 -0500 (Mon, 30 Jun 2008)
New Revision: 5325

Modified:
branches/1.1.x/numpy/ma/extras.py
Log:
implement itertools.groupby as a class

Modified: branches/1.1.x/numpy/ma/extras.py
===================================================================
--- branches/1.1.x/numpy/ma/extras.py	2008-06-29 03:37:04 UTC (rev 5324)
+++ branches/1.1.x/numpy/ma/extras.py	2008-06-30 07:11:53 UTC (rev 5325)
@@ -27,7 +27,6 @@
'vander','vstack',
]

-from itertools import groupby

import core
@@ -42,6 +41,28 @@
from numpy.lib.polynomial import _lstsq, _single_eps, _double_eps

#...............................................................................
+class groupby(object):
+    "Implements itertools.groupby for numpy 1.1.x"
+    def __init__(self, iterable, key=None):
+        if key is None:
+            key = lambda x: x
+        self.keyfunc = key
+        self.it = iter(iterable)
+        self.tgtkey = self.currkey = self.currvalue = xrange(0)
+    def __iter__(self):
+        return self
+    def next(self):
+        while self.currkey == self.tgtkey:
+            self.currvalue = self.it.next() # Exit on StopIteration
+            self.currkey = self.keyfunc(self.currvalue)
+        self.tgtkey = self.currkey
+        return (self.currkey, self._grouper(self.tgtkey))
+    def _grouper(self, tgtkey):
+        while self.currkey == tgtkey:
+            yield self.currvalue
+            self.currvalue = self.it.next() # Exit on StopIteration
+            self.currkey = self.keyfunc(self.currvalue)
+
def issequence(seq):
"""Is seq a sequence (ndarray, list or tuple)?"""
if isinstance(seq, ndarray):

```