Skip to main content

A lightweight Python library for comparing two dictionaries and getting only the differences.

Project description

Compare Dicts Lib 🛠️

A lightweight and efficient Python library for comparing two dictionaries and retrieving only the differences.

✨ Features

  • Compare two dictionaries and get only the changed keys.
  • Detect added, modified, and deleted keys.
  • Handle nested dictionaries automatically.
  • Ignore specific keys during comparison.
  • Flexible type checking (strict_type_checking=False to treat 1 and "1" as equal).
  • Works with lists of dictionaries, automatically matching elements by "id".
  • Apply a diff to update a dictionary with changes.

📦 Installation

pip install git+https://github.com/ton_github/compare_dicts_lib.git

🚀 Usage

🔍 Basic Comparison

from compare_dicts_lib import compare_dicts

old_data = {"name": "Alice", "age": 30}
new_data = {"name": "Alice", "age": 31, "email": "alice@example.com"}

diff = compare_dicts(new_data, old_data)
print(diff)

Output:

{"age": 31, "email": "alice@example.com"}

📑 Detailed Comparison (detailed=True)

from compare_dicts_lib import compare_dicts

old_data = {
    "name": "Alice",
    "age": 30,
    "address": {"zip": "75000", "city": "Paris"}
}
new_data = {
    "name": "Alice",
    "age": 31,
    "email": "alice@example.com",
    "address": {"city": "Paris"}
}

diff = compare_dicts(new_data, old_data, detailed=True)
print(diff)

Output:

{
    "age": {
        "type": "modified",
        "old_value": 30,
        "new_value": 31
    },
    "email": {
        "type": "added",
        "new_value": "alice@example.com"
    },
    "address": {
        "zip": {
            "type": "deleted",
            "old_value": "75000"
        }
    }
}

🎯 Ignoring Specific Keys (ignore_keys)

from compare_dicts_lib import compare_dicts

old_data = {
    "name": "Alice",
    "age": 30,
    "timestamp": "2024-03-13T10:00:00Z"
}
new_data = {
    "name": "Alice",
    "age": 31,
    "email": "alice@example.com",
    "timestamp": "2024-03-13T11:00:00Z"
}

diff = compare_dicts(new_data, old_data, ignore_keys=["timestamp"])
print(diff)

Output:

{"age": 31, "email": "alice@example.com"}

🔢 Disabling Strict Type Checking (strict_type_checking=False)

from compare_dicts_lib import compare_dicts

old_data = {"value": 1, "name": "Alice"}
new_data = {"value": "1", "name": "Alice"}

diff = compare_dicts(new_data, old_data, strict_type_checking=False)
print(diff)

Output:

{}

🛠️ Applying a Diff (apply_diff)

Basic Usage

from compare_dicts_lib import apply_diff

old_data = {"name": "Bob", "age": 30, "job": "developer"}
diff = {"name": "Robert", "age": None}

new_data = apply_diff(old_data, diff, detailed=False)
print(new_data)

Output:

{"name": "Robert", "job": "developer"}

📋 Applying a Detailed Diff (detailed=True)

from compare_dicts_lib import apply_diff

old_data = {
    "users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
}
diff = {
    "users": {
        "modified": [
            {
                "id": 2,
                "type": "modified",
                "old_value": {"id": 2, "name": "Bob"},
                "new_value": {"id": 2, "name": "Robert"}
            }
        ]
    }
}

new_data = apply_diff(old_data, diff, detailed=True)
print(new_data)

Output:

{
    "users": [
        {"id": 1, "name": "Alice"},
        {"id": 2, "name": "Robert"}
    ]
}

📖 API Reference

compare_dicts(new_dict, old_dict, detailed=False, ignore_keys=None, strict_type_checking=True) -> dict

Compares two dictionaries and returns only the differences.

Parameter Type Default Description
new_dict dict Required The updated dictionary.
old_dict dict Required The original dictionary.
detailed bool False If True, returns structured changes (added, modified, deleted).
ignore_keys list None Keys to exclude from the comparison.
strict_type_checking bool True If False, considers 1 and "1" as equal.

apply_diff(original_dict, diff, detailed=False) -> dict

Applies a diff to update an existing dictionary.

Parameter Type Default Description
original_dict dict Required The original dictionary to update.
diff dict Required The differences returned by compare_dicts().
detailed bool False If True, expects a structured diff with added, modified, and deleted.

👨‍💻 Contributing

If you find a bug or want to add features, feel free to submit a pull request!


📜 License

MIT License - Free to use and modify.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

compare_dicts_lib-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

compare_dicts_lib-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file compare_dicts_lib-0.1.0.tar.gz.

File metadata

  • Download URL: compare_dicts_lib-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for compare_dicts_lib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6eda19513d4871e2db935bb67a0c0a9750b2c1e07d6381b239b84ba035501c05
MD5 c9b80d6059f4d0b0ec11cf88e164f2f0
BLAKE2b-256 e99473937e832e432097c2c8b9421d4c02b8bfa20724afa042203a635d8724e3

See more details on using hashes here.

File details

Details for the file compare_dicts_lib-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for compare_dicts_lib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f49d4325910890e39df21ec92fa2a84472e734ca4c561abd21c812c67726039e
MD5 99eb65fb136fd618a0d8f816c46e2821
BLAKE2b-256 a9854e3a6a45153ce112f1ad7e504773ae2d149f22efb3f897a1c2e2edd5d2a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page