from typing import MutableMapping


def flatten(d, parent_key="", sep="."):
    """
    flatten a nested dictionary so that it can be more neatly loaded into a
    pandas dataframe

    https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys

    Parameters
    ----------
    d: dict
        the dictionary to flatten
    parent_key: str, optional
        in nested dicts, the name of the parent fields
    sep: str, optional
        the string used to concatenate parent and child field names

    Returns
    -------
    d: dict
        the flattened dictionary
    """
    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(v, new_key, sep=sep).items())
        else:
            items.append((new_key, v))
    return dict(items)