def update_dict()

in mobile_cv/arch/utils/helper.py [0:0]


def update_dict(dest, src, seq_func=None):
    """Update the dict 'dest' recursively.
    Elements in src could be a callable function with signature
        f(key, curr_dest_val)
    seq_func: function to handle how to process corresponding lists
        seq_func(key, src_val, dest_val) -> new_dest_val
        by default, list will be overrided
    """
    for key, val in src.items():
        if isinstance(val, collections.abc.Mapping):
            # dest[key] could be None in the case of a dict
            cur_dest = dest.get(key, {}) or {}
            assert isinstance(cur_dest, dict), cur_dest
            dest[key] = update_dict(cur_dest, val)
        elif (
            seq_func is not None
            and isinstance(val, collections.abc.Sequence)
            and not isinstance(val, str)
        ):
            cur_dest = dest.get(key, []) or []
            assert isinstance(cur_dest, list), cur_dest
            dest[key] = seq_func(key, val, cur_dest)
        else:
            if callable(val) and key in dest:
                dest[key] = val(key, dest[key])
            else:
                dest[key] = val
    return dest