9
def compat_tee(iterable):
11
Return two independent iterators from a single iterable.
13
Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html
15
# Note: Using a dictionary and a list as the default arguments here is
16
# deliberate and safe in this instance.
17
def gen(next, data={}, cnt=[0]):
19
for i in itertools.count():
21
item = data[i] = next()
26
next = iter(iterable).next
27
return gen(next), gen(next)
29
def groupby(iterable, keyfunc=None):
31
Taken from http://docs.python.org/lib/itertools-functions.html
35
iterable = iter(iterable)
37
lastkey = keyfunc(l[0])
48
# Not really in itertools, since it's a builtin in Python 2.4 and later, but it
49
# does operate as an iterator.
51
for index in xrange(len(data)-1, -1, -1):
54
if hasattr(itertools, 'tee'):
58
if hasattr(itertools, 'groupby'):
59
groupby = itertools.groupby
9
# Fallback for Python 2.4, Python 2.5
10
def product(*args, **kwds):
12
Taken from http://docs.python.org/library/itertools.html#itertools.product
14
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
15
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
16
pools = map(tuple, args) * kwds.get('repeat', 1)
19
result = [x+[y] for x in result for y in pool]
23
if hasattr(itertools, 'product'):
24
product = itertools.product
61
26
def is_iterable(x):
62
27
"A implementation independent way of checking for iterables"