# [mergedeep](https://mergedeep.readthedocs.io/en/latest/)
[![PyPi release](https://img.shields.io/pypi/v/mergedeep.svg)](https://pypi.org/project/mergedeep/)
[![PyPi versions](https://img.shields.io/pypi/pyversions/mergedeep.svg)](https://pypi.org/project/mergedeep/)
[![Downloads](https://pepy.tech/badge/mergedeep)](https://pepy.tech/project/mergedeep)
[![Conda Version](https://img.shields.io/conda/vn/conda-forge/mergedeep.svg)](https://anaconda.org/conda-forge/mergedeep)
[![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/mergedeep.svg)](https://anaconda.org/conda-forge/mergedeep)
[![Documentation Status](https://readthedocs.org/projects/mergedeep/badge/?version=latest)](https://mergedeep.readthedocs.io/en/latest/?badge=latest)
A deep merge function for 🐍.
[Check out the mergedeep docs](https://mergedeep.readthedocs.io/en/latest/)
## Installation
```bash
$ pip install mergedeep
```
## Usage
```text
merge(destination: MutableMapping, *sources: Mapping, strategy: Strategy = Strategy.REPLACE) -> MutableMapping
```
Deep merge without mutating the source dicts.
```python3
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.
```python3
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`
```python3
# 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]}
```
2. Additive
> `Strategy.ADDITIVE`
```python3
# 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})}
```
3. Typesafe replace
> `Strategy.TYPESAFE_REPLACE` or `Strategy.TYPESAFE`
```python3
# 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"
```
4. Typesafe additive
> `Strategy.TYPESAFE_ADDITIVE`
```python3
# 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**](https://blog.travismclarke.com/)
Raw data
{
"_id": null,
"home_page": "https://github.com/XieJiSS/mergedeep",
"name": "runch-mergedeep",
"maintainer": "XieJiSS",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "panrz@deepseek.com",
"keywords": null,
"author": "Travis Clarke",
"author_email": "travis.m.clarke@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a2/6a/6c215bd0e2c468a88e4bb89a31bc9c223cb26b93d5ab136fc2b78f1ec303/runch_mergedeep-1.3.5.tar.gz",
"platform": null,
"description": "# [mergedeep](https://mergedeep.readthedocs.io/en/latest/)\n\n[![PyPi release](https://img.shields.io/pypi/v/mergedeep.svg)](https://pypi.org/project/mergedeep/)\n[![PyPi versions](https://img.shields.io/pypi/pyversions/mergedeep.svg)](https://pypi.org/project/mergedeep/)\n[![Downloads](https://pepy.tech/badge/mergedeep)](https://pepy.tech/project/mergedeep)\n[![Conda Version](https://img.shields.io/conda/vn/conda-forge/mergedeep.svg)](https://anaconda.org/conda-forge/mergedeep)\n[![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/mergedeep.svg)](https://anaconda.org/conda-forge/mergedeep)\n[![Documentation Status](https://readthedocs.org/projects/mergedeep/badge/?version=latest)](https://mergedeep.readthedocs.io/en/latest/?badge=latest)\n\nA deep merge function for \ud83d\udc0d.\n\n[Check out the mergedeep docs](https://mergedeep.readthedocs.io/en/latest/)\n\n## Installation\n\n```bash\n$ pip install mergedeep\n```\n\n## Usage\n\n```text\nmerge(destination: MutableMapping, *sources: Mapping, strategy: Strategy = Strategy.REPLACE) -> MutableMapping\n```\n\nDeep merge without mutating the source dicts.\n\n```python3\nfrom mergedeep import merge\n\na = {\"keyA\": 1}\nb = {\"keyB\": {\"sub1\": 10}}\nc = {\"keyB\": {\"sub2\": 20}}\n\nmerged = merge({}, a, b, c) \n\nprint(merged)\n# {\"keyA\": 1, \"keyB\": {\"sub1\": 10, \"sub2\": 20}}\n```\n\nDeep merge into an existing dict.\n```python3\nfrom mergedeep import merge\n\na = {\"keyA\": 1}\nb = {\"keyB\": {\"sub1\": 10}}\nc = {\"keyB\": {\"sub2\": 20}}\n\nmerge(a, b, c) \n\nprint(a)\n# {\"keyA\": 1, \"keyB\": {\"sub1\": 10, \"sub2\": 20}}\n```\n\n### Merge strategies:\n\n1. Replace (*default*)\n\n> `Strategy.REPLACE`\n\n```python3\n# When `destination` and `source` keys are the same, replace the `destination` value with one from `source` (default).\n\n# Note: with multiple sources, the `last` (i.e. rightmost) source value will be what appears in the merged result. \n\nfrom mergedeep import merge, Strategy\n\ndst = {\"key\": [1, 2]}\nsrc = {\"key\": [3, 4]}\n\nmerge(dst, src, strategy=Strategy.REPLACE) \n# same as: merge(dst, src)\n\nprint(dst)\n# {\"key\": [3, 4]}\n```\n\n2. Additive\n\n> `Strategy.ADDITIVE`\n\n```python3\n# When `destination` and `source` values are both the same additive collection type, extend `destination` by adding values from `source`.\n# Additive collection types include: `list`, `tuple`, `set`, and `Counter`\n\n# Note: if the values are not additive collections of the same type, then fallback to a `REPLACE` merge.\n\nfrom mergedeep import merge, Strategy\n\ndst = {\"key\": [1, 2], \"count\": Counter({\"a\": 1, \"b\": 1})}\nsrc = {\"key\": [3, 4], \"count\": Counter({\"a\": 1, \"c\": 1})}\n\nmerge(dst, src, strategy=Strategy.ADDITIVE) \n\nprint(dst)\n# {\"key\": [1, 2, 3, 4], \"count\": Counter({\"a\": 2, \"b\": 1, \"c\": 1})}\n```\n\n3. Typesafe replace\n\n> `Strategy.TYPESAFE_REPLACE` or `Strategy.TYPESAFE`\n\n```python3\n# When `destination` and `source` values are of different types, raise `TypeError`. Otherwise, perform a `REPLACE` merge.\n\nfrom mergedeep import merge, Strategy\n\ndst = {\"key\": [1, 2]}\nsrc = {\"key\": {3, 4}}\n\nmerge(dst, src, strategy=Strategy.TYPESAFE_REPLACE) # same as: `Strategy.TYPESAFE` \n# TypeError: destination type: <class 'list'> differs from source type: <class 'set'> for key: \"key\"\n```\n\n4. Typesafe additive\n\n> `Strategy.TYPESAFE_ADDITIVE`\n\n```python3\n# When `destination` and `source` values are of different types, raise `TypeError`. Otherwise, perform a `ADDITIVE` merge.\n\nfrom mergedeep import merge, Strategy\n\ndst = {\"key\": [1, 2]}\nsrc = {\"key\": {3, 4}}\n\nmerge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE) \n# TypeError: destination type: <class 'list'> differs from source type: <class 'set'> for key: \"key\"\n```\n\n## License\n\nMIT © [**Travis Clarke**](https://blog.travismclarke.com/)\n",
"bugtrack_url": null,
"license": null,
"summary": "A deep merge function for \ud83d\udc0d.",
"version": "1.3.5",
"project_urls": {
"Homepage": "https://github.com/XieJiSS/mergedeep"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3f86c4d7d59a6de959dbd68765f6106fa96ad817927cc7fff5b827ca6149efe3",
"md5": "8cf86c1e1c7e8c4fd0dacce3dbc28cf0",
"sha256": "8213b361eb94e2233538ae62900f4f1b521b2ca5b5d24828c6531372496c81a4"
},
"downloads": -1,
"filename": "runch_mergedeep-1.3.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8cf86c1e1c7e8c4fd0dacce3dbc28cf0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 6487,
"upload_time": "2024-11-12T14:06:30",
"upload_time_iso_8601": "2024-11-12T14:06:30.411176Z",
"url": "https://files.pythonhosted.org/packages/3f/86/c4d7d59a6de959dbd68765f6106fa96ad817927cc7fff5b827ca6149efe3/runch_mergedeep-1.3.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a26a6c215bd0e2c468a88e4bb89a31bc9c223cb26b93d5ab136fc2b78f1ec303",
"md5": "3d319d0d181e2d05b06a852ea0e40964",
"sha256": "1fffae46252d5b8b9b97961e4a98bec20a3b65ab9d46024a92b213e4ff1e45c7"
},
"downloads": -1,
"filename": "runch_mergedeep-1.3.5.tar.gz",
"has_sig": false,
"md5_digest": "3d319d0d181e2d05b06a852ea0e40964",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 4450,
"upload_time": "2024-11-12T14:06:32",
"upload_time_iso_8601": "2024-11-12T14:06:32.082066Z",
"url": "https://files.pythonhosted.org/packages/a2/6a/6c215bd0e2c468a88e4bb89a31bc9c223cb26b93d5ab136fc2b78f1ec303/runch_mergedeep-1.3.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-12 14:06:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "XieJiSS",
"github_project": "mergedeep",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "runch-mergedeep"
}