深入合并python字典的工具集。
deepmerge的Python项目详细描述
处理合并的工具 python中的嵌套数据结构。
示例
一般策略
fromdeepmergeimportalways_mergerbase={"foo":["bar"]}next={"foo":["baz"]}expected_result={'foo':['bar','baz']}result=always_merger.merge(base,next)assertexpected_result==result
自定义策略
fromdeepmergeimportMergermy_merger=Merger(# pass in a list of tuple, with the# strategies you are looking to apply# to each type.[(list,["append"]),(dict,["merge"])],# next, choose the fallback strategies,# applied to all other types:["override"],# finally, choose the strategies in# the case where the types conflict:["override"])base={"foo":["bar"]}next={"bar":"baz"}my_merger.merge(base,next)assertbase=={"foo":["bar"],"bar":"baz"}
也可以传入自己的合并函数,而不是字符串。
有关详细信息,请参见docs
测试
$ pip install pytest $ pytest deepmerge/tests/