A deep merge function for 🐍.
Project description
mergedeep
A deep merge function for 🐍.
Installation
$ pip install mergedeep
Usage
merge(destination: 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 `destination` and `source` values are the same, replace the `destination` value with one from `source` (default).
# Note: with multiple sources, the `last` source value will be what appears in the merged result.
from mergedeep import merge, Strategy
dst = {"key": [1, 2]}
src = {"key": [3, 4]}
merge(dst, src, strategy=Strategy.REPLACE)
# same as: merge(dst, src)
print(dst)
# {"key": [3, 4]}
- Additive
# Strategy.ADDITIVE
# When `destination` and `source` values are both either `list`, `tuple`, or `set`, extend/update `destination` with values from `source` collection.
from mergedeep import merge, Strategy
dst = {"key": [1, 2]}
src = {"key": [3, 4]}
merge(dst, src, strategy=Strategy.ADDITIVE)
print(dst)
# {"key": [1, 2, 3, 4]}
- Typesafe replace
# Strategy.TYPESAFE_REPLACE or Strategy.TYPESAFE
# When `destination` and `source` values are of different types, raise `TypeError`. Otherwise, perform a `REPLACE` merge.
from mergedeep import merge, Strategy
dst = {"key": [1, 2]}
src = {"key": {3, 4}}
merge(dst, src, strategy=Strategy.TYPESAFE_REPLACE) # same as: `Strategy.TYPESAFE`
# TypeError: destination type: <class 'list'> differs from source type: <class 'set'> for key: "key"
- Typesafe additive
# Strategy.TYPESAFE_ADDITIVE
# When `destination` and `source` values are of different types, raise `TypeError`. Otherwise, perform a `ADDITIVE` merge.
from mergedeep import merge, Strategy
dst = {"key": [1, 2]}
src = {"key": {3, 4}}
merge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE)
# TypeError: destination 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.2.1.tar.gz
(4.4 kB
view details)
Built Distribution
File details
Details for the file mergedeep-1.2.1.tar.gz
.
File metadata
- Download URL: mergedeep-1.2.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f9c3f162d02cd0d8f932229c75f7e80b0faf09d66478450f71c48b8f9791e69 |
|
MD5 | 6e8f34402de47bf77d87bd00c0c6bc25 |
|
BLAKE2b-256 | 34ec5a63f2dc638450f5f145e6f22c302f5b979f648f5434c726a201dda49e47 |
Provenance
File details
Details for the file mergedeep-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: mergedeep-1.2.1-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c80d83ff746b24e2e0f4db94a9bd7ef0832a18050029b3e752298d5277435ad |
|
MD5 | e4912a85641492324564f9e0df1460a7 |
|
BLAKE2b-256 | 64abe3fcf10c038d17b7206f698c75b2a853106b7b407e3b017a27c6d94f2eef |