IcsUtils.JsonDiff Common Library

JsonDiff: Library for JSON DIFF

This is the JsonDiff common library.
class opslib.icsutils.jsondiff.Comparator(fp1=None, fp2=None, include=[], exclude=[], ignore_add=False)[source]

Main workhorse for JSON Comparator

__dict__ = dict_proxy({'__module__': 'opslib.icsutils.jsondiff', '_filter_results': <function _filter_results at 0x58b08c0>, '_compare_arrays': <function _compare_arrays at 0x58b0a28>, '_is_incex_key': <function _is_incex_key at 0x58b07d0>, '__init__': <function __init__ at 0x58b06e0>, '__dict__': <attribute '__dict__' of 'Comparator' objects>, '_compare_elements': <function _compare_elements at 0x58b0938>, '_compare_scalars': <function _compare_scalars at 0x58b09b0>, '__weakref__': <attribute '__weakref__' of 'Comparator' objects>, '__doc__': '\n Main workhorse for JSON Comparator\n ', 'compare_dicts': <function compare_dicts at 0x58b0aa0>})
__init__(fp1=None, fp2=None, include=[], exclude=[], ignore_add=False)[source]
Parameters:
  • fp1 (object) – file object (opened with read permission)
  • fp2 (object) – file object (opened with read permission)
  • include (list) – a list of attributes to include in the comparison
  • exclude (list) – a list of attributes to exclude in the comparison
  • ignore_add (bool) – whether to ignore the added items in the comparison

Example:

>>> from opslib.icsutils.jsondiff import Comparator
>>> import json
>>> old_json = {
...     "name": "opslib",
...     "version": "1.2.0",
...     "members": {
...         "role": "ops",
...         "group": [ "ops", "devops" ]
...     }
... }
>>> new_json = {
...     "name": "opslib",
...     "version": "1.3.0",
...     "members": {
...         "role": "devops",
...         "group": [ "devops" ]
...     }
... }
>>> json.dump(old_json, open("old.json", "w"))
>>> json.dump(new_json, open("new.json", "w"))
>>> fp_old = open("old.json", "r")
>>> fp_new = open("new.json", "r")
>>> engine = Comparator(fp_old, fp_new)
>>> res = engine.compare_dicts()
>>> print json.dumps(res, sort_keys=True, indent=4)
{
    "members": {
        "group": {
            "0": {
                "+++": "devops",
                "---": "ops"
            },
            "1": {
                "---": "devops"
            }
        },
        "role": {
            "+++": "devops",
            "---": "ops"
        }
    },
    "version": {
        "+++": "1.3.0",
        "---": "1.2.0"
    }
}
__module__ = 'opslib.icsutils.jsondiff'
__weakref__

list of weak references to the object (if defined)

_compare_arrays(old_arr, new_arr)[source]

simpler version of compare_dicts; just an internal method, because it could never be called from outside.

We have it guaranteed that both new_arr and old_arr are of type list.

_compare_elements(old, new)[source]

Unify decision making on the leaf node level.

_compare_scalars(old, new, name=None)[source]

Be careful with the result of this function. Negative answer from this function is really None, not False, so deciding based on the return value like in

if self._compare_scalars(...):

leads to wrong answer (it should be if self._compare_scalars(...) is not None:)

_filter_results(result)[source]

Whole -i or -x functionality. Rather than complicate logic while going through the object’s tree we filter the result of plain comparison.

Also clear out unused keys in result

_is_incex_key(key, value)[source]

Is this key excluded or not among included ones? If yes, it should be ignored.

compare_dicts(old_obj=None, new_obj=None)[source]

The real workhorse

opslib.icsutils.jsondiff.is_scalar(value)[source]

Primitive version, relying on the fact that JSON cannot contain any more complicated data structures.

Indices and tables

Read the Docs v: latest
Versions
latest
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.