Skip to main content

A deep merge function for 🐍.

Project description

mergedeep

PyPi release PyPi versions Downloads Documentation Status

A deep merge function for 🐍.

Check out the mergedeep docs

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:

  1. 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]}
  1. Additive
# Strategy.ADDITIVE
# When `target` and `source` values are both either `list` 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]}
  1. 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


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.0.3.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

mergedeep-1.0.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file mergedeep-1.0.3.tar.gz.

File metadata

  • Download URL: mergedeep-1.0.3.tar.gz
  • Upload date:
  • Size: 3.9 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.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2

File hashes

Hashes for mergedeep-1.0.3.tar.gz
Algorithm Hash digest
SHA256 4dd93dc022ff627c70c86461423c1af849cfe8412fa664553e60691af7adaed3
MD5 f96ba114e717be1fb7f1d5fc95a9bc54
BLAKE2b-256 e55ec607eea30613bb8180b71f0aee945188677ae99bf604f349df768ee610a1

See more details on using hashes here.

Provenance

File details

Details for the file mergedeep-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: mergedeep-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.3 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.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2

File hashes

Hashes for mergedeep-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9bf00d1e2b6cdf2f3db0d3edd7a66d5fee9d6dcc6e0a2ae296cbbb6fbfc5f067
MD5 5c622aa1c4aa5607803970cd952a7f47
BLAKE2b-256 00e2c75f6d0cb82e0d6821222c1686562609fd7f6be940a1081092b08721d723

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page