# DeepMerge
[![build status](https://img.shields.io/github/workflow/status/taliamax/pydeepmerge/build)](https://github.com/taliamax/pydeepmerge/actions?query=workflow%3Abuild) [![release status](https://img.shields.io/github/workflow/status/taliamax/pydeepmerge/release?label=release)](https://github.com/taliamax/pydeepmerge/actions?query=workflow%3Arelease) [![MIT license](https://img.shields.io/pypi/l/pydeepmerge)](https://github.com/taliamax/pydeepmerge/blob/master/LICENSE) [![coverage status](https://img.shields.io/coveralls/github/taliamax/pydeepmerge)](https://coveralls.io/github/taliamax/pydeepmerge)
[![pypi version](https://img.shields.io/pypi/v/pydeepmerge)](https://pypi.org/project/pydeepmerge/) [![pypi python versions supported](https://img.shields.io/pypi/pyversions/pydeepmerge)](https://pypi.org/project/pydeepmerge/) [![pypi downloads](https://img.shields.io/pypi/dm/pydeepmerge)](https://pypi.org/project/pydeepmerge/)
A lightweight python package for performing deep-merges of python dictionaries
## Installation
Install this package with pip!
```bash
$ pip install pydeepmerge
```
To install this package from source, clone the repo and run:
```bash
$ pip install .
```
If you would like to develop, remember to install the extras.
```bash
# for bash
$ pip install -e .[test,dev]
# for zsh
$ pip install -e .\[test,dev\]
```
## Usage
Usage is simple:
```python
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:
```python
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/taliamax/pydeepmerge",
"name": "pydeepmerge",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": "",
"keywords": "mapping dictionary library merge",
"author": "Natalia Maximo",
"author_email": "iam@natalia.dev",
"download_url": "https://files.pythonhosted.org/packages/89/5b/55b6eab1f2f14b07f62dd1f2e1f14eaa5d515afadf51c7f0468c90b00a4f/pydeepmerge-0.3.3.tar.gz",
"platform": "",
"description": "# DeepMerge\n\n[![build status](https://img.shields.io/github/workflow/status/taliamax/pydeepmerge/build)](https://github.com/taliamax/pydeepmerge/actions?query=workflow%3Abuild) [![release status](https://img.shields.io/github/workflow/status/taliamax/pydeepmerge/release?label=release)](https://github.com/taliamax/pydeepmerge/actions?query=workflow%3Arelease) [![MIT license](https://img.shields.io/pypi/l/pydeepmerge)](https://github.com/taliamax/pydeepmerge/blob/master/LICENSE) [![coverage status](https://img.shields.io/coveralls/github/taliamax/pydeepmerge)](https://coveralls.io/github/taliamax/pydeepmerge)\n\n[![pypi version](https://img.shields.io/pypi/v/pydeepmerge)](https://pypi.org/project/pydeepmerge/) [![pypi python versions supported](https://img.shields.io/pypi/pyversions/pydeepmerge)](https://pypi.org/project/pydeepmerge/) [![pypi downloads](https://img.shields.io/pypi/dm/pydeepmerge)](https://pypi.org/project/pydeepmerge/)\n\nA lightweight python package for performing deep-merges of python dictionaries\n\n## Installation\n\nInstall this package with pip!\n\n```bash\n$ pip install pydeepmerge\n```\n\nTo install this package from source, clone the repo and run:\n\n```bash\n$ pip install .\n```\n\nIf you would like to develop, remember to install the extras.\n\n```bash\n# for bash\n$ pip install -e .[test,dev]\n# for zsh\n$ pip install -e .\\[test,dev\\]\n```\n\n## Usage\n\nUsage is simple:\n\n```python\nfrom pydeepmerge import deep_merge\n\n> some_data = {'foo': {'bar': 'baz', 'spam': 'eggs'}, 'ham': 'eggs'}\n> more_data = {'spam': {'eggs': 'ham'}, 'foo': {'baz': 'bar', 'bar': 'foo'}}\n> deep_merge(some_data, more_data)\n{'foo': {'bar': 'foo', 'baz': 'bar', 'spam': 'eggs'}, 'spam': {'eggs': 'ham'}, 'ham': 'eggs'}\n```\n\n`pydeepmerge` also allows users to specify their own merge strategy function. By default, it uses the function `prefer_right`.\n\nA 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.\n\nWhen 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.\n\nAn example of a merge strategy function can be found below:\n\n```python\nfrom pydeepmerge import deep_merge\nfrom pydeepmerge.types import Key\nfrom typing import Mapping\n\ndef pick_shallower(left_value, right_value):\n if left_value is Key.NoKeyFound:\n return right_value\n\n if isinstance(right_value, Mapping):\n if not isinstance(left_value, Mapping):\n return left_value\n return deep_merge(left_value, right_value)\n\n return right_value\n```\n\nThe `deep_merge` function does not mutate any mapping but instead creates a new dictionary.\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A lightweight library to perform deep merges of python dictionaries",
"version": "0.3.3",
"project_urls": {
"Homepage": "https://github.com/taliamax/pydeepmerge"
},
"split_keywords": [
"mapping",
"dictionary",
"library",
"merge"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "15fdc3c86a5d14cfd17b47e0ba24afb398c61f4519672d22a435ba586fd974a3",
"md5": "8a8a7f55b1bf9fb867a4ba5f8cc40c5b",
"sha256": "e570fe4422a4448d21919c086f27d249dc8330684737160c02f5f35ba862ed90"
},
"downloads": -1,
"filename": "pydeepmerge-0.3.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8a8a7f55b1bf9fb867a4ba5f8cc40c5b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.5",
"size": 10309,
"upload_time": "2021-07-02T03:26:02",
"upload_time_iso_8601": "2021-07-02T03:26:02.970608Z",
"url": "https://files.pythonhosted.org/packages/15/fd/c3c86a5d14cfd17b47e0ba24afb398c61f4519672d22a435ba586fd974a3/pydeepmerge-0.3.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "895b55b6eab1f2f14b07f62dd1f2e1f14eaa5d515afadf51c7f0468c90b00a4f",
"md5": "349d2247b41ebafe566478db5810ba6f",
"sha256": "f8db9935b666b92baebdae53eaffb0be3c775f68df21c2cb86bbbafb3cbbb970"
},
"downloads": -1,
"filename": "pydeepmerge-0.3.3.tar.gz",
"has_sig": false,
"md5_digest": "349d2247b41ebafe566478db5810ba6f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 6530,
"upload_time": "2021-07-02T03:26:04",
"upload_time_iso_8601": "2021-07-02T03:26:04.063259Z",
"url": "https://files.pythonhosted.org/packages/89/5b/55b6eab1f2f14b07f62dd1f2e1f14eaa5d515afadf51c7f0468c90b00a4f/pydeepmerge-0.3.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-07-02 03:26:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "taliamax",
"github_project": "pydeepmerge",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pydeepmerge"
}