from six import iteritems


def merge(target, override):
    """ Recursive dict merge

    Based on merge method found in dcase_util > https://github.com/DCASE-REPO/dcase_util/blob/master/dcase_util/containers/containers.py#L366

    Parameters
    ----------
    target : dict
        target parameter dict
    override : dict
        override parameter dict

    Returns
    -------
    dict
    """

    for k, v in iteritems(override):
        if k in target and isinstance(target[k], dict) and isinstance(override[k], dict):
            target[k] = merge(target=target[k], override=override[k])

        else:
            target[k] = override[k]

    return target