def as_tuple(x, N):
    """
    Coerce a value to a tuple of length N.

    Parameters:
    -----------
    x : value or iterable
    N : integer
        length of the desired tuple

    Returns:
    --------
    tuple
        ``tuple(x)`` if `x` is iterable, ``(x,) * N`` otherwise.
    """
    try:
        X = tuple(x)
    except TypeError:
        X = (x,) * N

    if len(X) != N:
        raise ValueError("input must be a single value "
                         "or an iterable with length {0}".format(N))

    return X