Skip to main content

a compare utility

Project description

A Comparison package

Python 3.9+

License: MIT

This package is designed to compare two objects with a JSON-like structure and data types.

The package extends on the work done by Karpushkin Gleb.

Install

pip install -U pip espejo

Usage

First you need to define two variables: expected & actual. Think of them as the same variables that you use in tests.

Expected - the original data object that you want to see. Actual - the given data object.

Then we will transfer these objects to check and identify the difference between them:

from espejo import Compare, NO_DIFF


expected = {
    "project": {
        "name": "espejo",
        "version": "0.1",
        "license": "MIT",
        "language": {
            "name": "python",
            "versions": [
                3.5,
                3.6
            ]
        }
    },
    "os": "linux"
}

actual = {
    "project": {
        "name": "espejo",
        "version": 0.1,
        "license": "Apache 2.0",
        "language": {
            "name": "python",
            "versions": [
                3.6
            ]
        }
    }
}

diff = Compare().check(expected, actual)
assert diff != NO_DIFF

The check method returns a dictionary of differences between expected and actual objects:

{
    "project": {
        "version": {
            "_message": "Types not equal. Expected: <str>, received: <float>",
            "_expected": "str",
            "_received": "float"
        },
        "license": {
            "_message": "Values not equal. Expected: <MIT>, received: <Apache 2.0>",
            "_expected": "MIT",
            "_received": "Apache 2.0"
        },
        "language": {
            "versions": {
                "_length": {
                    "_message": "Lengths not equal. Expected <2>, received: <1>",
                    "_expected": 2,
                    "_received": 1
                },
                "_content": {
                    "0": {
                        "_message": "Value not found. Expected <3.5>",
                        "_expected": 3.5,
                        "_received": null
                    }
                }
            }
        }
    },
    "os": {
        "_message": "Key does not exists. Expected: <os>",
        "_expected": "os",
        "_received": null
    }
}

Configuration

The default configuration can be overridden by passing the config dictionary to the Compare class constructor:

from espejo import Compare

config = {
    "output": {
        "console": False,
        "file": {
            "allow_nan": True,
            "ensure_ascii": True,
            "indent": 4,
            "name": None,
            "skipkeys": True,
        },
    },
    "types": {
        "float": {
            "allow_round": 2,
        },
        "list": {
            "check_length": True,
        }
    }
}

cmp = Compare(config)

Output

By default, the configuration does not allow printing the comparison result to the console, but at the same time writes the results to a file.

These settings can be changed in your class config:

config = {
    "output": {
        "console": True,
        "file": {}
    }
}

Ignore rules

What if you do not want to compare some values and keys of objects from your JSON?
In this case, you can define exception rules and pass them to the class constructor.

Let's go back to the example above:

from espejo import Compare, NO_DIFF


expected = {
    # ...
}

actual = {
    # ...
}

rules = {
    "project": {
        "version": "*",
        "license": "*",
        "language": {
            "versions": {
                "_values": [
                    3.5
                ]
            }
        }
    },
    "os": "*",
}

diff = Compare(rules=rules).check(expected, actual)
assert diff == NO_DIFF

Now that we have added exceptions to the missing values, the comparison test has been successfully passed!

Links

You can see a more complex comparison example that I used to test the correct operation of an application: link.

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

espejo-0.1.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

espejo-0.1.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file espejo-0.1.2.tar.gz.

File metadata

  • Download URL: espejo-0.1.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/5.15.0-113-generic

File hashes

Hashes for espejo-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d5a39f4610fb51dfdd8a43daad4550d7ad39da91057f9b4f00bf84dd575f9243
MD5 4977fbd116b5f046de7feaf8d72aa6ef
BLAKE2b-256 d0dbbb6f0d33285ecb8b44f0f2fd23432c56c54095683d40024b4c7e5036ac57

See more details on using hashes here.

File details

Details for the file espejo-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: espejo-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/5.15.0-113-generic

File hashes

Hashes for espejo-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 382a5551cf82a44e865d26710b3d9524bc8acc24c14b15dd8369f894b969a57f
MD5 de18f57f8b80ffeb26c91cf4318e4767
BLAKE2b-256 1a8121206c7594a573fe0dcefba6a71230cf14f73ee24081b50d10884c12d91f

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