def as_scalar(val):
    """
    If val is iterable, this function returns the first entry
    else returns val. Handles strings as scalars
    :param val: A scalar or iterable
    :return:
    """
    # Check string
    if val == str(val):
        return val

    try:  # Check iterable
        it = iter(val)
        # Get first iterable
        return next(it)
    except TypeError:
        # Fallback, val is scalar
        return val