From 6580fc535f98f802c36347567d878f43333f8d18 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 4 Feb 2005 05:55:04 +0000 Subject: [PATCH] 2.4 defines groupby, so there's no need to define our own. --- src/utils/iter.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/utils/iter.py b/src/utils/iter.py index 6694e2c38..76e0debc4 100644 --- a/src/utils/iter.py +++ b/src/utils/iter.py @@ -27,6 +27,7 @@ # POSSIBILITY OF SUCH DAMAGE. ### +import sys import new import random @@ -48,21 +49,22 @@ def trueCycle(iterable): if not yielded: raise StopIteration -def groupby(key, iterable): - if key is None: - key = lambda x: x - it = iter(iterable) - value = it.next() # If there are no items, this takes an early exit - oldkey = key(value) - group = [value] - for value in it: - newkey = key(value) - if newkey != oldkey: - yield group - group = [] - oldkey = newkey - group.append(value) - yield group +if sys.version_info < (2, 4, 0): + def groupby(key, iterable): + if key is None: + key = lambda x: x + it = iter(iterable) + value = it.next() # If there are no items, this takes an early exit + oldkey = key(value) + group = [value] + for value in it: + newkey = key(value) + if newkey != oldkey: + yield group + group = [] + oldkey = newkey + group.append(value) + yield group def partition(p, iterable): """Partitions an iterable based on a predicate p.