espejo


Nameespejo JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://pypi.org/project/espejo
Summarya compare utility
upload_time2024-07-01 07:06:31
maintainerJorge Gueorguiev Garcia
docs_urlNone
authorGleb Karpushkin
requires_python<4.0,>=3.9
licenseMIT
keywords json compare
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## A Comparison package

<!-- [![Build Status](https://travis-ci.com/rugleb/JsonCompare.svg?branch=master)](https://travis-ci.com/rugleb/JsonCompare) -->
<!-- [![codecov](https://codecov.io/gh/rugleb/JsonCompare/branch/master/graph/badge.svg)](https://codecov.io/gh/rugleb/JsonCompare) -->
[![Python 3.9+](https://img.shields.io/badge/python-3.9+-green.svg)](https://www.python.org/downloads/release/python-360/)
<!-- [![PyPI version](https://badge.fury.io/py/espejo.svg)](https://badge.fury.io/py/espejo) -->
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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](https://github.com/rugleb).

### 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:

```python
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:

```json
{
    "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:

```python
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:

```py
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:

```python
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](https://github.com/twoormore/espejo/blob/master/tests/test_compare.py#L84).

            

Raw data

            {
    "_id": null,
    "home_page": "https://pypi.org/project/espejo",
    "name": "espejo",
    "maintainer": "Jorge Gueorguiev Garcia",
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": "jorge.gueorguiev@twoormore.eu",
    "keywords": "json, compare",
    "author": "Gleb Karpushkin",
    "author_email": "rugleb@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d0/db/bb6f0d33285ecb8b44f0f2fd23432c56c54095683d40024b4c7e5036ac57/espejo-0.1.2.tar.gz",
    "platform": null,
    "description": "## A Comparison package\n\n<!-- [![Build Status](https://travis-ci.com/rugleb/JsonCompare.svg?branch=master)](https://travis-ci.com/rugleb/JsonCompare) -->\n<!-- [![codecov](https://codecov.io/gh/rugleb/JsonCompare/branch/master/graph/badge.svg)](https://codecov.io/gh/rugleb/JsonCompare) -->\n[![Python 3.9+](https://img.shields.io/badge/python-3.9+-green.svg)](https://www.python.org/downloads/release/python-360/)\n<!-- [![PyPI version](https://badge.fury.io/py/espejo.svg)](https://badge.fury.io/py/espejo) -->\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nThis package is designed to compare two objects with a JSON-like structure and data types.\n\nThe package extends on the work done by [Karpushkin Gleb](https://github.com/rugleb).\n\n### Install\n\n```\npip install -U pip espejo\n```\n\n### Usage\n\nFirst you need to define two variables: `expected` & `actual`.\nThink of them as the same variables that you use in tests.\n\nExpected - the original data object that you want to see.\nActual - the given data object.\n\nThen we will transfer these objects to check and identify the difference between them:\n\n```python\nfrom espejo import Compare, NO_DIFF\n\n\nexpected = {\n    \"project\": {\n        \"name\": \"espejo\",\n        \"version\": \"0.1\",\n        \"license\": \"MIT\",\n        \"language\": {\n            \"name\": \"python\",\n            \"versions\": [\n                3.5,\n                3.6\n            ]\n        }\n    },\n    \"os\": \"linux\"\n}\n\nactual = {\n    \"project\": {\n        \"name\": \"espejo\",\n        \"version\": 0.1,\n        \"license\": \"Apache 2.0\",\n        \"language\": {\n            \"name\": \"python\",\n            \"versions\": [\n                3.6\n            ]\n        }\n    }\n}\n\ndiff = Compare().check(expected, actual)\nassert diff != NO_DIFF\n```\n\nThe `check` method returns a dictionary of differences between `expected` and `actual` objects:\n\n```json\n{\n    \"project\": {\n        \"version\": {\n            \"_message\": \"Types not equal. Expected: <str>, received: <float>\",\n            \"_expected\": \"str\",\n            \"_received\": \"float\"\n        },\n        \"license\": {\n            \"_message\": \"Values not equal. Expected: <MIT>, received: <Apache 2.0>\",\n            \"_expected\": \"MIT\",\n            \"_received\": \"Apache 2.0\"\n        },\n        \"language\": {\n            \"versions\": {\n                \"_length\": {\n                    \"_message\": \"Lengths not equal. Expected <2>, received: <1>\",\n                    \"_expected\": 2,\n                    \"_received\": 1\n                },\n                \"_content\": {\n                    \"0\": {\n                        \"_message\": \"Value not found. Expected <3.5>\",\n                        \"_expected\": 3.5,\n                        \"_received\": null\n                    }\n                }\n            }\n        }\n    },\n    \"os\": {\n        \"_message\": \"Key does not exists. Expected: <os>\",\n        \"_expected\": \"os\",\n        \"_received\": null\n    }\n}\n```\n\n### Configuration\n\nThe default configuration can be overridden by passing the config dictionary to the Compare class constructor:\n\n```python\nfrom espejo import Compare\n\nconfig = {\n    \"output\": {\n        \"console\": False,\n        \"file\": {\n            \"allow_nan\": True,\n            \"ensure_ascii\": True,\n            \"indent\": 4,\n            \"name\": None,\n            \"skipkeys\": True,\n        },\n    },\n    \"types\": {\n        \"float\": {\n            \"allow_round\": 2,\n        },\n        \"list\": {\n            \"check_length\": True,\n        }\n    }\n}\n\ncmp = Compare(config)\n```\n\n### Output\n\nBy default, the configuration does not allow printing the comparison result to the console,\nbut at the same time writes the results to a file.\n\n\nThese settings can be changed in your class config:\n\n```py\nconfig = {\n    \"output\": {\n        \"console\": True,\n        \"file\": {}\n    }\n}\n```\n\n### Ignore rules\n\nWhat if you do not want to compare some values and keys of objects from your JSON?  \nIn this case, you can define exception rules and pass them to the class constructor.\n\nLet's go back to the example above:\n\n```python\nfrom espejo import Compare, NO_DIFF\n\n\nexpected = {\n    # ...\n}\n\nactual = {\n    # ...\n}\n\nrules = {\n    \"project\": {\n        \"version\": \"*\",\n        \"license\": \"*\",\n        \"language\": {\n            \"versions\": {\n                \"_values\": [\n                    3.5\n                ]\n            }\n        }\n    },\n    \"os\": \"*\",\n}\n\ndiff = Compare(rules=rules).check(expected, actual)\nassert diff == NO_DIFF\n```\n\nNow that we have added exceptions to the missing values,\nthe comparison test has been successfully passed!\n\n### Links\n\nYou can see a more complex comparison example that I used to test the correct operation of an application:\n[link](https://github.com/twoormore/espejo/blob/master/tests/test_compare.py#L84).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "a compare utility",
    "version": "0.1.2",
    "project_urls": {
        "Documentation": "https://github.com/twoormore/espejo/blob/master/README.md",
        "Homepage": "https://pypi.org/project/espejo",
        "Repository": "https://github.com/twoormore/espejo"
    },
    "split_keywords": [
        "json",
        " compare"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1a8121206c7594a573fe0dcefba6a71230cf14f73ee24081b50d10884c12d91f",
                "md5": "de18f57f8b80ffeb26c91cf4318e4767",
                "sha256": "382a5551cf82a44e865d26710b3d9524bc8acc24c14b15dd8369f894b969a57f"
            },
            "downloads": -1,
            "filename": "espejo-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "de18f57f8b80ffeb26c91cf4318e4767",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 6973,
            "upload_time": "2024-07-01T07:06:30",
            "upload_time_iso_8601": "2024-07-01T07:06:30.532514Z",
            "url": "https://files.pythonhosted.org/packages/1a/81/21206c7594a573fe0dcefba6a71230cf14f73ee24081b50d10884c12d91f/espejo-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d0dbbb6f0d33285ecb8b44f0f2fd23432c56c54095683d40024b4c7e5036ac57",
                "md5": "4977fbd116b5f046de7feaf8d72aa6ef",
                "sha256": "d5a39f4610fb51dfdd8a43daad4550d7ad39da91057f9b4f00bf84dd575f9243"
            },
            "downloads": -1,
            "filename": "espejo-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "4977fbd116b5f046de7feaf8d72aa6ef",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 6093,
            "upload_time": "2024-07-01T07:06:31",
            "upload_time_iso_8601": "2024-07-01T07:06:31.749348Z",
            "url": "https://files.pythonhosted.org/packages/d0/db/bb6f0d33285ecb8b44f0f2fd23432c56c54095683d40024b4c7e5036ac57/espejo-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-01 07:06:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "twoormore",
    "github_project": "espejo",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "espejo"
}
        
Elapsed time: 0.26778s