Skip to main content

A lightweight library to perform deep merges of python dictionaries

Project description

DeepMerge

build status MIT license coverage status

pypi version pypi python versions supported pypi downloads

A lightweight python package for performing deep-merges of python dictionaries

Usage is simple:

from pydeepmerge import deep_merge

> some_data = {'foo': {'bar': 'baz', 'spam': 'eggs'}, 'ham': 'eggs'}
> more_data = {'spam': {'eggs': 'ham'}, 'foo': {'baz': 'bar', 'bar': 'foo'}}
> deep_merge(some_data, more_data)
{'foo': {'bar': 'foo', 'baz': 'bar', 'spam': 'eggs'}, 'spam': {'eggs': 'ham'}, 'ham': 'eggs'}

pydeepmerge also allows users to specify their own merge strategy function. By default, it uses the function prefer_right.

A merge strategy is any function that can accept exactly two inputs. The output of the merge strategy function should be what the merge result between two values should be.

When writing your own merge strategy function, keep in mind that if the key does not exist on the left-hand mapping, the value Key.NoKeyFound will be passed to the first parameter of the function. This is done deliberately so the user can determine if they want to do special behaviour if it is the first occurrence of the key in the sequence of dictionaries.

An example of a merge strategy function can be found below:

from pydeepmerge import deep_merge
from pydeepmerge.types import Key
from typing import Mapping

def pick_shallower(left_value, right_value):
    if left_value is Key.NoKeyFound:
        return right_value

    if isinstance(right_value, Mapping):
        if not isinstance(left_value, Mapping):
            return left_value
        return deep_merge(left_value, right_value)

    return right_value

The deep_merge function does not mutate any mapping but instead creates a new dictionary.

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

pydeepmerge-0.2.1b2.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

pydeepmerge-0.2.1b2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pydeepmerge-0.2.1b2.tar.gz.

File metadata

  • Download URL: pydeepmerge-0.2.1b2.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for pydeepmerge-0.2.1b2.tar.gz
Algorithm Hash digest
SHA256 4494b559bc6657b9f87a11184ef9d3ef1cac8b9c286ac0ba3590f412f91a52c9
MD5 5761fa938d265d17ebe5761849bbb091
BLAKE2b-256 ee705ff532a960fcf187458df56ab95fb1d7ce8062b99962dfff8b616a3cad61

See more details on using hashes here.

File details

Details for the file pydeepmerge-0.2.1b2-py3-none-any.whl.

File metadata

  • Download URL: pydeepmerge-0.2.1b2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for pydeepmerge-0.2.1b2-py3-none-any.whl
Algorithm Hash digest
SHA256 5e9c8542c5982fa5ba2b491c74ad2a9eb8bb96c2292b844c04b0ec7b9bd52f04
MD5 c4062d517272be6c191427e5b49fce37
BLAKE2b-256 dafe2e478023e72ccbb3ddcb225f80ebc43b7c3e50029452da1f57c01a4358ba

See more details on using hashes here.

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