import re
import sys

if sys.version_info.major == 3 and sys.version_info.minor >= 10:
    from collections.abc import MutableMapping
else:
    from collections import MutableMapping


def clean_column_name(column_name):
    return re.sub('\W', '_', column_name.lower())


def flatten_record(d, parent_key='', sep='__'):
    items = []
    for k, v in d.items():
        new_key = parent_key + sep + k if parent_key else k
        if isinstance(v, MutableMapping):
            items.extend(flatten_record(v, new_key, sep=sep).items())
        else:
            items.append((new_key, str(v) if type(v) is list else v))

    return dict(items)
