def merge_dicts()

in alibabacloud_oss_v2/utils.py [0:0]


def merge_dicts(dict1, dict2, append_lists=False):
    """Given two dict, merge the second dict into the first.
    The dicts can have arbitrary nesting.

    Args:
        dict1 (Dict): the first dict.
        dict2 (Dict): the second dict.
        append_lists (bool, optional): If true, instead of clobbering a list with the new
        value, append all of the new values onto the original list.
    """

    for key in dict2:
        if isinstance(dict2[key], dict):
            if key in dict1 and key in dict2:
                merge_dicts(dict1[key], dict2[key])
            else:
                dict1[key] = dict2[key]
        # If the value is a list and the ``append_lists`` flag is set,
        # append the new values onto the original list
        elif isinstance(dict2[key], list) and append_lists:
            # The value in dict1 must be a list in order to append new
            # values onto it.
            if key in dict1 and isinstance(dict1[key], list):
                dict1[key].extend(dict2[key])
            else:
                dict1[key] = dict2[key]
        else:
            # At scalar types, we iterate and merge the
            # current dict that we're on.
            dict1[key] = dict2[key]