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.7.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.7-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_dict_merge-0.1.7.tar.gz
Algorithm Hash digest
SHA256 37cb28841579528349b83519284a80245f6d6843c4fae1ebb6090239027a557b
MD5 2c59fb319558a85571621c058c022401
BLAKE2b-256 630768fb08ef3e6734798cd41fca1e7cffaf48a364b5ab5c5ec2c9578f57d588

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_dict_merge-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 543464243d1f7a0ee72b5e029a5960cde6db133c93b73307c4485c691cac5b26
MD5 08abf83d5af139cb6320c82f791c6424
BLAKE2b-256 6feb2cc2fdd4546989341c1e9044c6a16e48b18a16183bb5756bcbb281e90eec

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