App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / django / utils / itercompat.py
blob82434b7c9b8bf7975d2b7a6d178b2452e86fc463
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 """
7 import __builtin__
8 import itertools
9 import warnings
11 # Fallback for Python 2.5
12 def product(*args, **kwds):
13 """
14 Taken from http://docs.python.org/library/itertools.html#itertools.product
15 """
16 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
17 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
18 pools = map(tuple, args) * kwds.get('repeat', 1)
19 result = [[]]
20 for pool in pools:
21 result = [x+[y] for x in result for y in pool]
22 for prod in result:
23 yield tuple(prod)
25 if hasattr(itertools, 'product'):
26 product = itertools.product
28 def is_iterable(x):
29 "A implementation independent way of checking for iterables"
30 try:
31 iter(x)
32 except TypeError:
33 return False
34 else:
35 return True
37 def all(iterable):
38 warnings.warn("django.utils.itercompat.all is deprecated; use the native version instead",
39 PendingDeprecationWarning)
40 return __builtin__.all(iterable)
42 def any(iterable):
43 warnings.warn("django.utils.itercompat.any is deprecated; use the native version instead",
44 PendingDeprecationWarning)
45 return __builtin__.any(iterable)