import itertools


def permutate(collection, r=None):
    """Generates all the permutations of a given iterable, of a given length.

    It is essentially a wrapper around the built-in ``itertools.permutations``.

    :param iterable collection: The iterable to permutate.
    :param int r: The number of elements to arange. If not given, it will be\
    assumed to be equal to the length of the collection.
    :raises ValueError: if r is greater than the length of the collection.
    :rtype: ``tuple``"""

    n = len(tuple(collection))
    if r is not None and r > n:
        raise ValueError("r {} is larger than n {}".format(r, n))
    return itertools.permutations(collection, r=r)