import re
import unicodedata

__all__ = ["module_to_dict", "str_to_identifier"]


def module_to_dict(module):
    """Mimics IMPORT_STAR behavior.

    Allows all symbols from the module not starting with '_'
    """
    return {key: getattr(module, key) for key in dir(module) if not key.startswith("_")}


def str_to_identifier(somestring):
    """Make a valid Python identifier from a string.

    See https://stackoverflow.com/questions/3303312/how-do-i-convert-a-string-to-a-valid-variable-name-in-python

    >>> str_to_identifier('32v2 äßß #Gmw845éh$W b53wi- ')
    '_32v2_a__Gmw845eh_W_b53wi__'
    """
    interpolated_string = (
        unicodedata.normalize("NFKD", somestring)
        .encode("ascii", "ignore")
        .decode("ascii")
    )

    return re.sub(r"\W|^(?=\d)", "_", interpolated_string)
