import six


def to_snake(val):
    """Takes a string and transforms it to snake_case."""

    if not isinstance(val, six.string_types):
        raise ValueError

    return "".join(["_" + x.lower() if x.isupper() else x for x in val])