def to_tuple(input, *, tuple_len=2):
    """Convert to tuple of given length.

    This utility function is used to convert single-value arguments to tuples
    of appropriate length, e.g. for multi-dimensional inputs where each
    dimension requires the same value. If the argument is already an iterable
    it is returned as a tuple if its length matches the desired tuple length.
    Otherwise a `ValueError` is raised.

    Args:
        input (non-iterable or iterable): argument to be converted to tuple
        tuple_len (int): required length of argument tuple. Default: `2`

    Returns:
        tuple: tuple of desired length

    Example:
        >>> to_tuple(2)
        (2, 2)

    """
    try:
        iter(input)
        if len(input) != tuple_len:
            raise ValueError(
                f'Input length expected to be {tuple_len} but was {len(input)}'
                )
        else:
            input = tuple(input)
    except TypeError:
        input = tuple([input] * tuple_len)
    return input
