![]() ![]() Now lists will be merged, checks and raises an exception if both dicts have a key in common but different data types, and the method takes 2 or more dicts to merge instead of just two. I like some of changes to implementation, but neither version respects/merges lists, so I hybridized the two and added a few things. Raise Exception('New keys added when they should not be')Īssert dict_merge(a, b, add_keys=False) = 6 """Will it avoid inserting new keys when required?"""Īssert dict_merge(a, b, add_keys=False) = 1Īssert dict_merge(a, b, add_keys=False) = 3Īssert dict_merge(a, b, add_keys=False) = 4Īssert dict_merge(a, b, add_keys=False) = 5 """Will it insert new keys by default?""" If (k in dct and isinstance(dct, dict)Īnd isinstance(merge_dct, collections.Mapping)):ĭct = dict_merge(dct, merge_dct, add_keys=add_keys) Present in ``merge_dict`` but not ``dct`` should be included in theĭct (dict) onto which the merge is executedįor k in set(dct).intersection(set(merge_dct)) The optional argument ``add_keys``, determines whether keys which are ![]() This version will return a copy of the dictionary and leave the original from unittest import TestCaseĭef dict_merge(dct, merge_dct, add_keys=True): Here's a Python 3 version with a test case that: a) returns a new dictionary rather than updating the old ones, and b) controls whether to add in keys from merge_dct which are not in dct. If ( k in dct and isinstance( dct, dict) and isinstance( merge_dct, dict)): #noqa :param dct: dict onto which the merge is executed Updating only top-level keys, dict_merge recurses down into dicts nested Inspired by :meth:``dict.update()``, instead of # See the License for the specific language governing permissions and # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # distributed under the License is distributed on an "AS IS" BASIS, # Unless required by applicable law or agreed to in writing, software # You may obtain a copy of the License at # you may not use this file except in compliance with the License. # Licensed under the Apache License, Version 2.0 (the "License")
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |