summaryrefslogtreecommitdiff
path: root/django/utils/itercompat.py
diff options
context:
space:
mode:
authorChristopher Long <indirecthit@gmail.com>2007-06-17 22:18:54 +0000
committerChristopher Long <indirecthit@gmail.com>2007-06-17 22:18:54 +0000
commitae22b6d403dcf25098c77f0dfcf59ae58b186461 (patch)
treec37fc631e99a7e4d909d6b6d236f495003731ea7 /django/utils/itercompat.py
parent0cf7bc439129c66df8d64601e885f83b256b4f25 (diff)
per-object-permissions: Merged to trunk [5486] NOTE: Not fully tested, will be working on this over the next few weeks.
git-svn-id: http://code.djangoproject.com/svn/django/branches/per-object-permissions@5488 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/itercompat.py')
-rw-r--r--django/utils/itercompat.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py
index 370988bedb..48a2d99d3a 100644
--- a/django/utils/itercompat.py
+++ b/django/utils/itercompat.py
@@ -7,7 +7,8 @@ these implementations if necessary.
import itertools
def compat_tee(iterable):
- """Return two independent iterators from a single iterable.
+ """
+ Return two independent iterators from a single iterable.
Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html
"""
@@ -25,7 +26,28 @@ def compat_tee(iterable):
next = iter(iterable).next
return gen(next), gen(next)
+def groupby(iterable, keyfunc=None):
+ """
+ Taken from http://docs.python.org/lib/itertools-functions.html
+ """
+ if keyfunc is None:
+ keyfunc = lambda x:x
+ iterable = iter(iterable)
+ l = [iterable.next()]
+ lastkey = keyfunc(l)
+ for item in iterable:
+ key = keyfunc(item)
+ if key != lastkey:
+ yield lastkey, l
+ lastkey = key
+ l = [item]
+ else:
+ l.append(item)
+ yield lastkey, l
+
if hasattr(itertools, 'tee'):
tee = itertools.tee
else:
tee = compat_tee
+if hasattr(itertools, 'groupby'):
+ groupby = itertools.groupby