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`, `tuple`, 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.

Files for mergedeep, version 1.1.1
Filename, size File type Python version Upload date Hashes
Filename, size mergedeep-1.1.1-py3-none-any.whl (5.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size mergedeep-1.1.1.tar.gz (4.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page