onedict


Nameonedict JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/flusflas/onedict
SummaryA Python library for recursively merging dictionaries with customizable conflict resolution strategies.
upload_time2024-11-02 12:15:09
maintainerNone
docs_urlNone
authorflusflas
requires_python<4.0,>=3.9
licenseMIT
keywords dictionaries merge
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <h2 align="center"><b>onedict</b></h3>

<p align="center">A Python library for recursively merging
dictionaries with customizable conflict resolution strategies</p>

<p align="center">
  <a href="https://pypi.org/project/onedict"><img src="https://img.shields.io/badge/pip_install-onedict-orange" alt="pip command"></a>
  <a href="https://pypi.org/project/onedict"><img src="https://img.shields.io/pypi/pyversions/onedict.svg?logo=python" alt="Supported Versions"></a>
  <a href="https://codecov.io/gh/flusflas/onedict"><img src="https://codecov.io/gh/flusflas/onedict/graph/badge.svg" alt="codecov"></a>
</p>

## What is *onedict*?

**onedict** is a Python library that provides a simple way to merge multiple
dictionaries with customizable conflict resolution strategies. It allows you to
merge dictionaries with nested structures and provides built-in solvers for
common conflict resolution strategies.

## Installation

```sh
pip install onedict
```

## Usage

### Basic Usage

To merge two or more dictionaries:

```python
from onedict.merger import merge

dict1 = {"info": {"version": "1.0.0", "author": "Alice"}}
dict2 = {"info": {"license": "MIT"}, "data": {"value": 42}}

merged = merge(dict1, dict2)  # More dictionaries can be added as arguments
print(merged)
# Output: {'info': {'version': '1.0.0', 'author': 'Alice', 'license': 'MIT'}, 'data': {'value': 42}}
```

### Handling Conflicts

When merging dictionaries, conflicts may arise when two dictionaries have the
same key with different values. By default, a `MergeConflictError` exception is
raised when a conflict is detected:

```python
from onedict.merger import merge, MergeConflictError

dict1 = {"foo": "bar"}
dict2 = {"foo": "baz"}

try:
    merged = merge(dict1, dict2)   # Raises MergeConflictError
except MergeConflictError as e:
    print(e)
```

To handle conflicts, you can provide a list of conflict solvers to the `merge` function:

```python
def custom_solver(keys, value1, value2):
    return value1  # Keep the value from the first dictionary

merged = merge(dict1, dict2, conflict_solvers=[custom_solver])
print(merged)  # Output: {'foo': 'bar'}
```

Conflict solvers are added to the `conflict_solvers` list in the order they are
provided. The first solver that returns a non-`Skip` value is used to resolve
the conflict. If none of the solvers can resolve the conflict, a
`MergeConflictError` is raised.

### Built-in Solvers

onedict provides built-in solvers for common conflict resolution strategies:

```python
from onedict.merger import merge
from onedict.solvers import unique_lists

merged = merge(
    {"foo": ["bar", "baz"]},
    {"foo": ["bar", "qux"]},
    conflict_solvers=[unique_lists]
)
print(merged)  # Output: {'foo': ['bar', 'baz', 'qux']}
```

The following built-in solvers are available:

| Solver Name           | Description                                                        |
|-----------------------|--------------------------------------------------------------------|
| `unique_lists`        | Merges lists by combining unique elements from both lists.         |
| `concatenate_strings` | Merges two strings by concatenating them with a separator.         |
| `keep_original`       | Keeps the original value and discards the new one.                 |
| `keep_new`            | Keeps the new value and discards the original one.                 |

### Custom Conflict Solvers

You can create custom conflict solvers to handle specific types of conflicts:

```python
from onedict.merger import merge, Skip

def adder(keys, value1, value2):
    if isinstance(value1, int) and isinstance(value2, int):
        return value1 + value2
    return Skip()

merged = merge(
    {"foo": 1},
    {"foo": 2},
    conflict_solvers=[adder]
)
print(merged)  # Output: {'foo': 3}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/flusflas/onedict",
    "name": "onedict",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "dictionaries, merge",
    "author": "flusflas",
    "author_email": "aflusflas@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/29/43/620d69c9a7b4677d58dbfa2192cc49918252127636bc4ab0263c27674f56/onedict-0.1.0.tar.gz",
    "platform": null,
    "description": "<h2 align=\"center\"><b>onedict</b></h3>\n\n<p align=\"center\">A Python library for recursively merging\ndictionaries with customizable conflict resolution strategies</p>\n\n<p align=\"center\">\n  <a href=\"https://pypi.org/project/onedict\"><img src=\"https://img.shields.io/badge/pip_install-onedict-orange\" alt=\"pip command\"></a>\n  <a href=\"https://pypi.org/project/onedict\"><img src=\"https://img.shields.io/pypi/pyversions/onedict.svg?logo=python\" alt=\"Supported Versions\"></a>\n  <a href=\"https://codecov.io/gh/flusflas/onedict\"><img src=\"https://codecov.io/gh/flusflas/onedict/graph/badge.svg\" alt=\"codecov\"></a>\n</p>\n\n## What is *onedict*?\n\n**onedict** is a Python library that provides a simple way to merge multiple\ndictionaries with customizable conflict resolution strategies. It allows you to\nmerge dictionaries with nested structures and provides built-in solvers for\ncommon conflict resolution strategies.\n\n## Installation\n\n```sh\npip install onedict\n```\n\n## Usage\n\n### Basic Usage\n\nTo merge two or more dictionaries:\n\n```python\nfrom onedict.merger import merge\n\ndict1 = {\"info\": {\"version\": \"1.0.0\", \"author\": \"Alice\"}}\ndict2 = {\"info\": {\"license\": \"MIT\"}, \"data\": {\"value\": 42}}\n\nmerged = merge(dict1, dict2)  # More dictionaries can be added as arguments\nprint(merged)\n# Output: {'info': {'version': '1.0.0', 'author': 'Alice', 'license': 'MIT'}, 'data': {'value': 42}}\n```\n\n### Handling Conflicts\n\nWhen merging dictionaries, conflicts may arise when two dictionaries have the\nsame key with different values. By default, a `MergeConflictError` exception is\nraised when a conflict is detected:\n\n```python\nfrom onedict.merger import merge, MergeConflictError\n\ndict1 = {\"foo\": \"bar\"}\ndict2 = {\"foo\": \"baz\"}\n\ntry:\n    merged = merge(dict1, dict2)   # Raises MergeConflictError\nexcept MergeConflictError as e:\n    print(e)\n```\n\nTo handle conflicts, you can provide a list of conflict solvers to the `merge` function:\n\n```python\ndef custom_solver(keys, value1, value2):\n    return value1  # Keep the value from the first dictionary\n\nmerged = merge(dict1, dict2, conflict_solvers=[custom_solver])\nprint(merged)  # Output: {'foo': 'bar'}\n```\n\nConflict solvers are added to the `conflict_solvers` list in the order they are\nprovided. The first solver that returns a non-`Skip` value is used to resolve\nthe conflict. If none of the solvers can resolve the conflict, a\n`MergeConflictError` is raised.\n\n### Built-in Solvers\n\nonedict provides built-in solvers for common conflict resolution strategies:\n\n```python\nfrom onedict.merger import merge\nfrom onedict.solvers import unique_lists\n\nmerged = merge(\n    {\"foo\": [\"bar\", \"baz\"]},\n    {\"foo\": [\"bar\", \"qux\"]},\n    conflict_solvers=[unique_lists]\n)\nprint(merged)  # Output: {'foo': ['bar', 'baz', 'qux']}\n```\n\nThe following built-in solvers are available:\n\n| Solver Name           | Description                                                        |\n|-----------------------|--------------------------------------------------------------------|\n| `unique_lists`        | Merges lists by combining unique elements from both lists.         |\n| `concatenate_strings` | Merges two strings by concatenating them with a separator.         |\n| `keep_original`       | Keeps the original value and discards the new one.                 |\n| `keep_new`            | Keeps the new value and discards the original one.                 |\n\n### Custom Conflict Solvers\n\nYou can create custom conflict solvers to handle specific types of conflicts:\n\n```python\nfrom onedict.merger import merge, Skip\n\ndef adder(keys, value1, value2):\n    if isinstance(value1, int) and isinstance(value2, int):\n        return value1 + value2\n    return Skip()\n\nmerged = merge(\n    {\"foo\": 1},\n    {\"foo\": 2},\n    conflict_solvers=[adder]\n)\nprint(merged)  # Output: {'foo': 3}\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python library for recursively merging dictionaries with customizable conflict resolution strategies.",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/flusflas/onedict",
        "Repository": "https://github.com/flusflas/onedict"
    },
    "split_keywords": [
        "dictionaries",
        " merge"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "83af396aabc421f71b65b6b26825e2a57edb0dc720f8fd60c058a41031b0a6e3",
                "md5": "437ec091b2af3400add1f5b6f22d35e7",
                "sha256": "367a91515cfcd04246231f4d0d5f40774a22b0465fcc561c056331466fbb640f"
            },
            "downloads": -1,
            "filename": "onedict-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "437ec091b2af3400add1f5b6f22d35e7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 5472,
            "upload_time": "2024-11-02T12:15:08",
            "upload_time_iso_8601": "2024-11-02T12:15:08.575854Z",
            "url": "https://files.pythonhosted.org/packages/83/af/396aabc421f71b65b6b26825e2a57edb0dc720f8fd60c058a41031b0a6e3/onedict-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2943620d69c9a7b4677d58dbfa2192cc49918252127636bc4ab0263c27674f56",
                "md5": "374d9bff1734b908fa67e7ef4712597b",
                "sha256": "7833d947f1d1da4839e93e33d3df75a3de3e713869a1052197950ddfe6ebd7bd"
            },
            "downloads": -1,
            "filename": "onedict-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "374d9bff1734b908fa67e7ef4712597b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 4554,
            "upload_time": "2024-11-02T12:15:09",
            "upload_time_iso_8601": "2024-11-02T12:15:09.713955Z",
            "url": "https://files.pythonhosted.org/packages/29/43/620d69c9a7b4677d58dbfa2192cc49918252127636bc4ab0263c27674f56/onedict-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-02 12:15:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "flusflas",
    "github_project": "onedict",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "onedict"
}
        
Elapsed time: 5.77167s