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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: mergedeep-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 542cf4b0361e529113ddf5eb46fbe0b6dca33caed9ad2cad72d79817e693b103
MD5 9f4b383688dc82d8163c0fe909b67987
BLAKE2b-256 f57374328ffb4c800d368985110e528b32b0275bc8854631791660d191627602

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: mergedeep-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 99b8f8fe970899ba335cabfc5fa55911119cce5f7b3c4b23489a4ab50f09e438
MD5 b8ea234ae87791d1f553cbed80565e62
BLAKE2b-256 b8f90539e1d417d6a24b0483bd028f85ae74ece04d9253ffcea4276dc88649ac

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