Skip to main content

Deep merge dictionaries safely with conflict resolution

Project description

philiprehberger-dict-merge

Tests PyPI version Last updated

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

# Custom resolution per key
merge(
    {"a": 1, "b": 5},
    {"a": 4, "b": 2},
    strategy=Strategy.CALLBACK,
    on_conflict=lambda key, left, right: max(left, right),
)
# {"a": 4, "b": 5}

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
Strategy.CALLBACK Resolve conflicts via on_conflict(key, left, right)
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

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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.2.0.tar.gz (6.3 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.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_dict_merge-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bca31d8071c44f80d9f833b59bd3c2e5e4e1dabc6c113616f20a7f110beb1c94
MD5 5c785db90ceab44869a4398a0e80dd1c
BLAKE2b-256 aa405f10f8a1f12577149266d8d5be33698f6b04e5f6fded1e2a70ebb6f579cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_dict_merge-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a784073787ff6a06e0e98a6971e705dc6085b4c0157b028c9104ac4b995e3c46
MD5 f9a9dbdfb2a165ae27668cf2fd15f57d
BLAKE2b-256 e1bad87e02e6317d1b18bb099173e016b1e549c3fcf23a12d9107ed3b78e5382

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