def flat_mapping_of_dict(d, sep='_', prefix=''):
    """
    Return a copy of d, recursively unpacking all dicts.

    Note: This assumes all keys are strings!

    :param d: a dict
    :param sep: the prefix to a key
    :param prefix: the prefix to keys in this dict
    :return: a new dict
    """
    new_d = {}
    for k, v in d.items():
        k = prefix + sep + str(k) if prefix else str(k)

        if isinstance(v, dict):
            new_d.update(flat_mapping_of_dict(v, sep=sep, prefix=k))
        else:
            new_d[k] = v
    return new_d