2006-09-26 14:33:32 +08:00
|
|
|
"""
|
|
|
|
Providing iterator functions that are not in all version of Python we support.
|
|
|
|
Where possible, we try to use the system-native version and only fall back to
|
|
|
|
these implementations if necessary.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import itertools
|
2011-03-30 16:34:05 +08:00
|
|
|
import warnings
|
2006-09-26 14:33:32 +08:00
|
|
|
|
2011-06-10 04:01:28 +08:00
|
|
|
# Fallback for Python 2.5
|
2009-12-22 23:18:51 +08:00
|
|
|
def product(*args, **kwds):
|
|
|
|
"""
|
|
|
|
Taken from http://docs.python.org/library/itertools.html#itertools.product
|
|
|
|
"""
|
|
|
|
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
|
|
|
|
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
|
|
|
|
pools = map(tuple, args) * kwds.get('repeat', 1)
|
|
|
|
result = [[]]
|
|
|
|
for pool in pools:
|
|
|
|
result = [x+[y] for x in result for y in pool]
|
|
|
|
for prod in result:
|
|
|
|
yield tuple(prod)
|
|
|
|
|
|
|
|
if hasattr(itertools, 'product'):
|
|
|
|
product = itertools.product
|
2007-09-15 03:55:24 +08:00
|
|
|
|
|
|
|
def is_iterable(x):
|
|
|
|
"A implementation independent way of checking for iterables"
|
|
|
|
try:
|
|
|
|
iter(x)
|
|
|
|
except TypeError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2011-03-30 16:34:05 +08:00
|
|
|
|
|
|
|
def all(iterable):
|
|
|
|
warnings.warn("django.utils.itercompat.all is deprecated; use the native version instead",
|
|
|
|
PendingDeprecationWarning)
|
|
|
|
for item in iterable:
|
|
|
|
if not item:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def any(iterable):
|
|
|
|
warnings.warn("django.utils.itercompat.any is deprecated; use the native version instead",
|
|
|
|
PendingDeprecationWarning)
|
|
|
|
for item in iterable:
|
|
|
|
if item:
|
|
|
|
return True
|
|
|
|
return False
|