跳转到主要内容

用于深度合并Python字典的工具集。

项目描述

https://img.shields.io/pypi/v/deepmerge.svg https://img.shields.io/pypi/status/deepmerge.svg https://img.shields.io/pypi/pyversions/pillar.svg https://img.shields.io/github/license/toumorokoshi/deepmerge.svg https://github.com/toumorokoshi/deepmerge/actions/workflows/python-package.yaml/badge.svg

处理Python中嵌套数据结构合并的工具。

安装

deepmerge可在pypi上找到

pip install deepmerge

示例

通用策略

from deepmerge import always_merger

base = {"foo": ["bar"]}
next = {"foo": ["baz"]}

expected_result = {'foo': ['bar', 'baz']}
result = always_merger.merge(base, next)

assert expected_result == result

自定义策略

from deepmerge import Merger

my_merger = Merger(
    # pass in a list of tuple, with the
    # strategies you are looking to apply
    # to each type.
    [
        (list, ["append"]),
        (dict, ["merge"]),
        (set, ["union"])
    ],
    # 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)
assert base == {"foo": ["bar"], "bar": "baz"}

您也可以传入自己的合并函数,而不是字符串。

有关更多信息,请参阅文档

支持版本

deepmerge支持Python 3.8+。

对于较旧的Python版本,以下是deepmerge的最后一个支持版本

  • 3.7 : 1.1.1

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

deepmerge-2.0.tar.gz (19.9 kB 查看哈希值)

上传时间 源代码

构建分发

deepmerge-2.0-py3-none-any.whl (13.5 kB 查看哈希值)

上传时间 Python 3

支持