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` (i.e. rightmost) 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 the same additive collection type, extend `destination` by adding values from `source`.
# Additive collection types include: `list`, `tuple`, `set`, and `Counter`
# Note: if the values are not additive collections of the same type, then fallback to a `REPLACE` merge.
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
orStrategy.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.3.0.tar.gz
(4.6 kB
view details)
Built Distribution
File details
Details for the file mergedeep-1.3.0.tar.gz
.
File metadata
- Download URL: mergedeep-1.3.0.tar.gz
- Upload date:
- Size: 4.6 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 | 19a91123e71ae27cb22335f4d03aec040026c89d4ff6df42595f7e7223a83dfb |
|
MD5 | 299d5625188cb41d6e7fe56c91841060 |
|
BLAKE2b-256 | 50ff6c0b3817037c21fc46cb0eabd97152da6bb5bb33b1bfeaba1e7cc865a8d2 |
Provenance
File details
Details for the file mergedeep-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: mergedeep-1.3.0-py3-none-any.whl
- Upload date:
- Size: 6.3 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 | 0a700d0cdf7589333b89111a61ed66b484c51690156471da80a7f54fcc2638b5 |
|
MD5 | 49d70fde8f59ee978bcc6b6b05cb12d3 |
|
BLAKE2b-256 | 0bd037c8262105accce4e81719649645066583d8c3ec62732c4cc8efc565546c |