Dictdiffer: Diff and Patch Python dictionaries
github.com
github.com
See Guido's "dictionary views" described at: http://www.python.org/dev/peps/pep-3106/ In Python 2.7, those dictionary views are exposed as d.viewkeys(), d.viewitems(), and d.viewvalues(). The key and item views both support set operations such as union, intersection, and difference. Also note, the dict() constructor will accept sequences of items as input. Those tool make it trivially easy to express diffing and patching in native Python:
# diff from d to e
patch = (d.viewitems() - e.viewitems(), # deletions
e.viewitems() - d.viewitems()) # additions
# apply the patch to f
# dict(f.viewitems() - patch[0] | patch[1])I haven't looked, but I guess it is also recursive on values that are dictionaries themselves. In this case the "patch" is a sort of sequence of edit operations on the dictionary tree.
I have written at least two dict differs, and I suspect they are like web frameworks - everyone has written a half working one.
However we need good tools that get polished and standardised - this looks nice and I like the full circle ability, but in python's own version of Catch-22, until it succeeds I will stick with writing my own.
I hope it succeeds - my meware sucks
I'm getting ambitious now, but what about tree-edit distance (with patch script) for members which are nested lists?
There is Python code out there for these:
https://github.com/timtadh/zhang-shasha
https://code.google.com/p/py-editdist
EDIT I have only used the above libraries for distances, not for patchable diffs. But the Levenshtein and tree-edit distance algorithms are amenable to outputting the patch scripts.
Here's a Python implementation: https://github.com/stefankoegl/python-json-patch