Cycle through given iterable specific (or infinite) number of times.

    :param n: Number of cycles.
              If None, result cycles through ``iterable`` indefinitely.

    :return: Iterable that cycles through given one

    .. note::

        This is an extended version of ncycles() recipe
        from the :module:`itertools` module documentation
        that also has the functionality of standard :func:`itertools.cycle`.

def cycle(iterable, n=None):
    """Cycle through given iterable specific (or infinite) number of times.

    :param n: Number of cycles.
              If None, result cycles through ``iterable`` indefinitely.

    :return: Iterable that cycles through given one

    .. note::

        This is an extended version of ncycles() recipe
        from the :module:`itertools` module documentation
        that also has the functionality of standard :func:`itertools.cycle`.
    """
    ensure_iterable(iterable)

    if n is None:
        return cycle_(iterable)
    else:
        if not isinstance(n, Integral):
            raise TypeError("invalid number of cycles")
        if n < 0:
            raise ValueError("number of cycles cannot be negative")

        return chain.from_iterable(repeat(tuple(iterable), n))