Skip to main content

Deep merge dictionaries safely with conflict resolution

Project description

philiprehberger-dict-merge

Tests PyPI version License

Deep merge dictionaries safely with conflict resolution.

Installation

pip install philiprehberger-dict-merge

Usage

from philiprehberger_dict_merge import merge, Strategy

base = {"db": {"host": "localhost", "port": 5432}, "debug": False}
override = {"db": {"port": 3306, "name": "mydb"}, "debug": True}

merge(base, override)
# {"db": {"host": "localhost", "port": 3306, "name": "mydb"}, "debug": True}

# Multiple dicts
merge(defaults, config_file, env_overrides)

# Keep first value on conflict
merge(a, b, strategy=Strategy.KEEP_FIRST)

# Append lists instead of replacing
merge(a, b, list_strategy="append")

# Raise on conflict
from philiprehberger_dict_merge import MergeConflictError

try:
    merge({"key": 1}, {"key": 2}, strategy=Strategy.ERROR)
except MergeConflictError as e:
    print(e.key)   # "key"
    print(e.left)  # 1
    print(e.right) # 2

API

Function / Class Description
merge(*dicts, strategy=Strategy.REPLACE, list_strategy="replace") Deep merge
Strategy.REPLACE Later values win (default)
Strategy.KEEP_FIRST Earlier values win
Strategy.ERROR Raise on conflict
MergeConflictError Raised by Strategy.ERROR — has .key, .left, .right attributes
List strategies: "replace", "append", "unique", "concat" List merge modes

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_dict_merge-0.1.8.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_dict_merge-0.1.8-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_dict_merge-0.1.8.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_dict_merge-0.1.8.tar.gz
Algorithm Hash digest
SHA256 9b3eb5f87295b4a6a067a073158a3c6dbd6721b88d3a5ac043c36e655eb6dcd5
MD5 604e9fc3d6987d24ab82603b508ccd5d
BLAKE2b-256 0aedb7e8e64fb1661e6b076fddc868995f50c03f97eff408d72720d57da5f50a

See more details on using hashes here.

File details

Details for the file philiprehberger_dict_merge-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_dict_merge-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 7a8687378ca0c8944ba95811e79c64a3893fc269331cfb4b9e7bd03b335a99e8
MD5 a03c12115682c0eda79f66b8a0117a39
BLAKE2b-256 6babd494dcd02cbc2c5f72f2f6ff682a50add1a510e40d8bcf1d272b578e018f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page