def diff( *, normalize: bool, sort_keys: bool, skip_empty: bool, left: dict, right: dict, n: int, input_format: str, output_format: str = "diff", verbose: bool = False ): """diff dict""" from dictknife.diff import diff, diff_rows, make_jsonpatch with open(left) as rf: left_data = loading.load(rf, format=input_format) with open(right) as rf: right_data = loading.load(rf, format=input_format) if output_format == "diff": for line in diff( left_data, right_data, fromfile=left, tofile=right, n=n, normalize=normalize, sort_keys=sort_keys, ): print(line) elif output_format == "jsonpatch": r = make_jsonpatch(left_data, right_data, verbose=verbose) loading.dumpfile(list(r), format="json") else: if output_format == "dict": output_format = "json" diff_key = "diff" rows = diff_rows( left_data, right_data, fromfile=left, tofile=right, diff_key=diff_key, normalize=normalize, ) if skip_empty: rows = [row for row in rows if row[diff_key] not in ("", 0)] loading.dumpfile(rows, format=output_format)
def _callFUT(self, d0, right): from dictknife.diff import diff_rows return diff_rows(d0, right)
from dictknife.diff import diff_rows d0 = { "x": 10, "y": 100, "nested": { "v1": 0, "v2": 1, "vs": [0, 1, 2, 3], }, } d1 = { "x": 10, "y": 110, "nested": { "v1": 10, "v2": -1, "vs": [0, 2, 2, 2], }, } for row in diff_rows(d0, d1): if row["diff"] in ("", 0): continue print(row)