summaryrefslogtreecommitdiff
path: root/lib/python2.7/site-packages/django/utils/itercompat.py
blob: c50dcfb779258dcf2a3016e3e712a3afd0066048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
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 collections
import itertools
import sys
import warnings


def is_iterable(x):
    "A implementation independent way of checking for iterables"
    try:
        iter(x)
    except TypeError:
        return False
    else:
        return True

def is_iterator(x):
    """An implementation independent way of checking for iterators

    Python 2.6 has a different implementation of collections.Iterator which
    accepts anything with a `next` method. 2.7+ requires and `__iter__` method
    as well.
    """
    if sys.version_info >= (2, 7):
        return isinstance(x, collections.Iterator)
    return isinstance(x, collections.Iterator) and hasattr(x, '__iter__')

def product(*args, **kwds):
    warnings.warn("django.utils.itercompat.product is deprecated; use the native version instead",
                  DeprecationWarning, stacklevel=2)
    return itertools.product(*args, **kwds)