Skip to main content

mergedeep

PyPi release PyPi versions Downloads Conda Version Conda Downloads Documentation Status

A deep merge function for 🐍.

Check out the mergedeep docs

Installation

$ pip install mergedeep

Usage

merge(destination: MutableMapping, *sources: Mapping, strategy: Strategy = Strategy.REPLACE) -> MutableMapping

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 `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]}
  1. 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})}
  1. 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"
  1. 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

runch_mergedeep-1.3.5.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

runch_mergedeep-1.3.5-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file runch_mergedeep-1.3.5.tar.gz.

File metadata

  • Download URL: runch_mergedeep-1.3.5.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for runch_mergedeep-1.3.5.tar.gz
Algorithm Hash digest
SHA256 1fffae46252d5b8b9b97961e4a98bec20a3b65ab9d46024a92b213e4ff1e45c7
MD5 3d319d0d181e2d05b06a852ea0e40964
BLAKE2b-256 a26a6c215bd0e2c468a88e4bb89a31bc9c223cb26b93d5ab136fc2b78f1ec303

See more details on using hashes here.

File details

Details for the file runch_mergedeep-1.3.5-py3-none-any.whl.

File metadata

File hashes

Hashes for runch_mergedeep-1.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8213b361eb94e2233538ae62900f4f1b521b2ca5b5d24828c6531372496c81a4
MD5 8cf86c1e1c7e8c4fd0dacce3dbc28cf0
BLAKE2b-256 3f86c4d7d59a6de959dbd68765f6106fa96ad817927cc7fff5b827ca6149efe3

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.3.5 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page