A deep merge function for 🐍.
Project description
mergedeep
A deep merge function for 🐍.
Installation
$ pip install mergedeep
Usage
merge(target: Map[KT, VT], *sources: Map[KT, VT], strategy: Strategy = Strategy.REPLACE) -> Map[KT, VT]
Deep merge without mutating the source dicts.
from mergedeep import merge
a = {"keyA": 1}
b = {"keyB": {"sub1": 10}}
c = {"keyB": {"sub2": 20}}
merged = merge({}, a, b, c)
print(merged)
# {"keyA": 1, "keyB": {"sub1": 10, "sub2": 20}}
Deep merge into an existing dict.
from mergedeep import merge
a = {"keyA": 1}
b = {"keyB": {"sub1": 10}}
c = {"keyB": {"sub2": 20}}
merge(a, b, c)
print(a)
# {"keyA": 1, "keyB": {"sub1": 10, "sub2": 20}}
Merge strategies:
- Replace (default)
# Strategy.REPLACE
# When `target` and `source` values are the same, replace the `target` value with one from `source` (default).
# Note: with multiple sources, the `last` source value with be what appears in the merged result.
from mergedeep import merge, Strategy
target = {"key": [1, 2]}
source = {"key": [3, 4]}
merge(target, source, strategy=Strategy.REPLACE)
# same as: merge(target, source)
print(target)
# {"key": [3, 4]}
- Additive
# Strategy.ADDITIVE
# When `target` and `source` values are both either `list`, `tuple`, or `set`, extend/update `target` with values from `source` collection.
from mergedeep import merge, Strategy
target = {"key": [1, 2]}
source = {"key": [3, 4]}
merge(target, source, strategy=Strategy.ADDITIVE)
print(target)
# {"key": [1, 2, 3, 4]}
- Typesafe
# Strategy.TYPESAFE
# When `target` and `source` values are of different types, raise `TypeError`.
from mergedeep import merge, Strategy
target = {"key": [1, 2]}
source = {"key": {3, 4}}
merge(target, source, strategy=Strategy.TYPESAFE)
# TypeError: target type: <class 'list'> differs from source type: <class 'set'> for key: "key"
License
MIT © Travis Clarke
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
mergedeep-1.1.1.tar.gz
(4.0 kB
view details)
Built Distribution
File details
Details for the file mergedeep-1.1.1.tar.gz
.
File metadata
- Download URL: mergedeep-1.1.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 609438501039d3832f797f50a0bce0ac61e29f3ce7679c12778baed1d0be4c3d |
|
MD5 | 9734924fdb991ded8dae206bd48c9dca |
|
BLAKE2b-256 | e412c9c93285c47a23debdc4fb810d5bdf8b1d900fc7b5fbce62fff686647596 |
Provenance
File details
Details for the file mergedeep-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: mergedeep-1.1.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e688378c3eb3d1297dac3a56f4a1b0d4cfec004629688bf35f01cedde479ad1f |
|
MD5 | 03b6048479bfc00306b593829d0e81ff |
|
BLAKE2b-256 | 8a362c2eb770468c96cf642eda7b62601817f63fd899d3bb0ea9a9627f36f269 |