from typing import MutableMapping


def flatten_dict(unflattend_dict, parent_key='', sep='.', keep_unflattend=False):
    """Flattens an nested dictionary to a one-level dictionary.

    Parameters
    ----------
    unflattend_dict: dict or MutableMapping
        A dictionary with nested dictionaries
    parent_key: str
        A key to which the flattened keys are added. Mainly present to support flatting of subdicts, should be '' in
        most primary calls.. Defaults to ''.
    sep: str
        The separator used for the flat items. Defaults to '.'
    keep_unflattend: bool
        Whether the keys, value pairs of nested dicts or MutableMappings should remain present in the output or not.
        Defaults to False

    Returns
    -------
    dict
        A dictionary with one (flattened) key for a value.
    """
    items = []
    for key, value in unflattend_dict.items():
        new_key = parent_key + sep + key if parent_key else key
        if isinstance(value, MutableMapping):
            if keep_unflattend:
                items.append((new_key, value))
            items.extend(flatten_dict(value, new_key, sep=sep).items())
        else:
            items.append((new_key, value))
    return dict(items)