|
38
|
1 |
""" |
|
|
2 |
Providing iterator functions that are not in all version of Python we support. |
|
|
3 |
Where possible, we try to use the system-native version and only fall back to |
|
|
4 |
these implementations if necessary. |
|
|
5 |
""" |
|
|
6 |
|
|
|
7 |
import itertools |
|
|
8 |
|
|
|
9 |
# Fallback for Python 2.4, Python 2.5 |
|
|
10 |
def product(*args, **kwds): |
|
|
11 |
""" |
|
|
12 |
Taken from http://docs.python.org/library/itertools.html#itertools.product |
|
|
13 |
""" |
|
|
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) |
|
|
17 |
result = [[]] |
|
|
18 |
for pool in pools: |
|
|
19 |
result = [x+[y] for x in result for y in pool] |
|
|
20 |
for prod in result: |
|
|
21 |
yield tuple(prod) |
|
|
22 |
|
|
|
23 |
if hasattr(itertools, 'product'): |
|
|
24 |
product = itertools.product |
|
|
25 |
|
|
|
26 |
def is_iterable(x): |
|
|
27 |
"A implementation independent way of checking for iterables" |
|
|
28 |
try: |
|
|
29 |
iter(x) |
|
|
30 |
except TypeError: |
|
|
31 |
return False |
|
|
32 |
else: |
|
|
33 |
return True |
|
|
34 |
|
|
|
35 |
def all(iterable): |
|
|
36 |
for item in iterable: |
|
|
37 |
if not item: |
|
|
38 |
return False |
|
|
39 |
return True |