/lib/django-1.5/django/utils/itercompat.py

https://github.com/theosp/google_appengine · Python · 46 lines · 28 code · 7 blank · 11 comment · 5 complexity · e9e8ed7ab3ff99817d02098d1d032dea MD5 · raw file

  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. import collections
  7. import itertools
  8. import sys
  9. import warnings
  10. def is_iterable(x):
  11. "A implementation independent way of checking for iterables"
  12. try:
  13. iter(x)
  14. except TypeError:
  15. return False
  16. else:
  17. return True
  18. def is_iterator(x):
  19. """An implementation independent way of checking for iterators
  20. Python 2.6 has a different implementation of collections.Iterator which
  21. accepts anything with a `next` method. 2.7+ requires and `__iter__` method
  22. as well.
  23. """
  24. if sys.version_info >= (2, 7):
  25. return isinstance(x, collections.Iterator)
  26. return isinstance(x, collections.Iterator) and hasattr(x, '__iter__')
  27. def product(*args, **kwds):
  28. warnings.warn("django.utils.itercompat.product is deprecated; use the native version instead",
  29. PendingDeprecationWarning)
  30. return itertools.product(*args, **kwds)
  31. def all(iterable):
  32. warnings.warn("django.utils.itercompat.all is deprecated; use the native version instead",
  33. DeprecationWarning)
  34. return builtins.all(iterable)
  35. def any(iterable):
  36. warnings.warn("django.utils.itercompat.any is deprecated; use the native version instead",
  37. DeprecationWarning)
  38. return builtins.any(iterable)