Skip to main content

onedict

A Python library for recursively merging dictionaries with customizable conflict resolution strategies

pip command Supported Versions codecov

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

pip install onedict

Usage

Basic Usage

To merge two or more dictionaries:

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:

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:

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:

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:

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}

Metadata

Release files for onedict 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for onedict 0.1.0
File Size Uploaded Provenance Actions
onedict-0.1.0.tar.gz 4.6 kB Without provenance Details for onedict-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for onedict 0.1.0
File Interpreter ABI Platform Actions
onedict-0.1.0-py3-none-any.whl Python 3 none any Details for onedict-0.1.0-py3-none-any.whl

Total release size: 10.0 kB

Release files / onedict-0.1.0.tar.gz

Download URL onedict-0.1.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7833d947f1d1da4839e93e33d3df75a3de3e713869a1052197950ddfe6ebd7bd
BLAKE2b-256 checksum
How to use checksums
2943620d69c9a7b4677d58dbfa2192cc49918252127636bc4ab0263c27674f56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Linux/6.2.0-39-generic

Release files / onedict-0.1.0-py3-none-any.whl

Download URL onedict-0.1.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
367a91515cfcd04246231f4d0d5f40774a22b0465fcc561c056331466fbb640f
BLAKE2b-256 checksum
How to use checksums
83af396aabc421f71b65b6b26825e2a57edb0dc720f8fd60c058a41031b0a6e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Linux/6.2.0-39-generic

Security for onedict 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page