import re


def safe_name(name, quotes=True):
    """
    Get a table name that BigQuery would accept by removing all bad chars
    and making it lowercase
    """
    name = name.replace('`', '')
    pattern = '[^a-zA-Z0-9]'
    removed_bad_chars = re.sub(pattern, '_', name).lower()
    if quotes:
        return '`{}`'.format(removed_bad_chars)
    return removed_bad_chars