Skip to main content

A library for deep comparison of data structures consisting of `dict`, `list` and `tuple`.

Project description

python-deepcompare

PyPI Test Status Codecov

python-deepcompare is a library to deep compare data structures with each other. It can check if two data structures contain the same data, or if a data structure is a subset of another data structure. The library supports Sequence (e.g. list or tuple) and Mapping (e.g. dict) types for the deep comparison.

Installation

With a correctly configured pipenv toolchain:

pipenv install python-deepcompare

You may also use classic pip to install the package:

pip install python-deepcompare

Getting started

How it works

  • As a default, the comparison treats all Sequence and all Mapping types the same (e.g. (1, 2, 3) is equal to [1, 2, 3]). To enable strict type checks, use the strict keyword argument.
  • The partial_compare method checks if the data structure given as the second parameter is a subset of the data structure given as the first parameter.
    • For Mapping types this means, that all keys of the second data structure are also keys on the first data structure, and the values of the keys are also equal (e.g. {'a': 1, 'b': 2} is a subset of {'a': 1, 'b': 2, 'c': 3}, but {'a': 1, 'b': 2, 'd': 4} is not).
    • For Sequence types this means, that all values of the second data structure are also values of the first data structure, and the values are in the same order (e.g. [1, 3, 5] is a subset of [1, 2, 3, 4, 5], but [1, 5, 3] is not).

Usage

import deepcompare

# test if two data structures are equal, but the types to not need to match exactly
deepcompare.compare(
    {'key1': (1, 2, 3), 'key2': {'key3': [4, 5, 6]}},
    {'key1': [1, 2, 3], 'key2': {'key3': (4, 5, 6)}},
)  # returns: True

# test if two data structures are equal, and make sure the types match exactly
deepcompare.compare(
    {'key1': (1, 2, 3), 'key2': {'key3': [4, 5, 6]}},
    {'key1': [1, 2, 3], 'key2': {'key3': (4, 5, 6)}},
    strict=True,
)  # returns: False

# test if the second data structure is contained within the first, but 
# the types to not need to match exactly
deepcompare.partial_compare(
    {'key1': (1, 2, 3), 'key2': {'key3': [4, 5, 6]}, 'key4': True},
    {'key1': [1, 2], 'key2': {'key3': (4, 6)}},
)  # returns: True

# test if the second data structure is contained within the first, and 
# make sure the types match exactly
deepcompare.partial_compare(
    {'key1': (1, 2, 3), 'key2': {'key3': [4, 5, 6]}, 'key4': True},
    {'key1': [1, 2], 'key2': {'key3': (4, 6)}},
    strict=True,
)  # returns: False

List of developers

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

python-deepcompare-1.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

python_deepcompare-1.0.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file python-deepcompare-1.0.1.tar.gz.

File metadata

  • Download URL: python-deepcompare-1.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for python-deepcompare-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e44e0536a94c4c97257c37e13b4b8f0a20a72e5596ea13bc4b70656caa2bd457
MD5 e5c8b6be715854e39ac7bb779eaed339
BLAKE2b-256 40e0c7e4d9d7a5a6cb99fa2823b5f746881b385866a20e996288db04a8986a4b

See more details on using hashes here.

File details

Details for the file python_deepcompare-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_deepcompare-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for python_deepcompare-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8a8dfc61427bd295ffb84c3d8f07f7e2770eaba197177456664e5d7c4ac3b48f
MD5 8a739933b6c9f81ae25a58cc29dfe5ee
BLAKE2b-256 04f1e97b8774c16ea23aa4c8ea64a930a4f99ffb87aeb96409145573d01af186

See more details on using hashes here.

Supported by

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