A deep merge function for 🐍.
Project description
mergedeep
A deep merge function for 🐍.
Installation
$ pip install mergedeep
Usage
merge(destination: Dict, *sources: Dict, strategy: Strategy = Strategy.REPLACE) -> Dict
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` keys 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], "count": Counter({"a": 1, "b": 1})}
src = {"key": [3, 4], "count": Counter({"a": 1, "c": 1})}
merge(dst, src, strategy=Strategy.ADDITIVE)
print(dst)
# {"key": [1, 2, 3, 4], "count": Counter({"a": 2, "b": 1, "c": 1})}
- 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.2.tar.gz
(4.6 kB
view hashes)
Built Distribution
Close
Hashes for mergedeep-1.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fec890313864a546699aa482c86b6030ac980eaa3e7aff346bea9e52f829e6ec |
|
MD5 | ec105aef095a291eaf2211263b6f46ed |
|
BLAKE2b-256 | 0831552cae9484e5ea482997c3077fc0e668a28fa8bdf3b92e49d558a9bb468a |