from typing import Iterable
from typing import Any
from typing import Optional
from typing import Iterator
from typing import Tuple
import itertools


def enumerate_cycle(
        iterable: Iterable[Any],
        *,
        times: Optional[int] = None,
) -> Iterator[Tuple[int, Any]]:
    """Return iterator of tuples indicating cycle count and item.

    The passed iterable is assumed to be finite.

    Arguments:
        iterable: finite iterable to be repeated

    Keyword Arguments:
        times: integral number of times to repeat the iterable (optional;
            default is None, signifying the cycle should be repeated
            infinitely)

    Returns:
        iterator of tuples consisting of the number of the cycle and each item
        of the passed iterable, repeating the cycle the specified number
        of times (or infinitely, if no number of times is specified)

    Examples:

    >>> pattern = 'hee haw'.split()
    >>> list(enumerate_cycle(pattern, times=3))
    [(0, 'hee'), (0, 'haw'), (1, 'hee'), (1, 'haw'), (2, 'hee'), (2, 'haw')]

    """
    iterable = tuple(iterable)
    if not iterable:
        return iter(())
    counter = itertools.count() if times is None else range(times)
    return ((i, item) for i in counter for item in iterable)