Deep merge dictionaries safely with conflict resolution
Project description
philiprehberger-dict-merge
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
Support
If you find this project useful:
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_dict_merge-0.1.9.tar.gz.
File metadata
- Download URL: philiprehberger_dict_merge-0.1.9.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9d82305d70842361b25d9862c7878e204d09f3701032242e08cf7eab1c06175
|
|
| MD5 |
68178cc572ec0f633c100a60246930cd
|
|
| BLAKE2b-256 |
7ce1470f7ed488379a7a1e6292ef957edd6adfc6e414f6c532e7288cb90ef81a
|
File details
Details for the file philiprehberger_dict_merge-0.1.9-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_dict_merge-0.1.9-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0908ad96e60d6537053bc30f724ca5a734d26ad13c638ccc57f4be9cd5c48a81
|
|
| MD5 |
e20f3ecb4d7e6faf7dbb7a99021bcac5
|
|
| BLAKE2b-256 |
e9524f9ebc68fc1ca9a12879ecac23505d136b03eb5d32d332de328093f90b4a
|