A lightweight library to perform deep merges of python dictionaries
Project description
DeepMerge
A lightweight python package for performing deep-merges of python dictionaries
Installation
Install this package with pip!
$ pip install pydeepmerge
To install this package from source, clone the repo and run:
$ pip install .
If you would like to develop, remember to install the extras.
# for bash
$ pip install -e .[test,dev]
# for zsh
$ pip install -e .\[test,dev\]
Usage
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydeepmerge-0.3.2.tar.gz
.
File metadata
- Download URL: pydeepmerge-0.3.2.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ca62fe9b30109244a78c16441e142b000e289fea46038d27151e208cc967178 |
|
MD5 | 68356cd305637989dc06e4989374f662 |
|
BLAKE2b-256 | 39b0ea4785f036e97d4187eff7be655676891035ee9581c1c4b2e9144b9e3651 |
File details
Details for the file pydeepmerge-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: pydeepmerge-0.3.2-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08aaf189a147293389e7c91a186e1e82a9c99d3f5be67a0e8962b85c4ee85f3c |
|
MD5 | c8009ec794187e5b0140a36725da581f |
|
BLAKE2b-256 | 0f2977ea0b41857c833e4acc09bb57be3124f9063b3d8d2ef7a8a8fd437a9a21 |