TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Dictdiffer: Diff and Patch Python dictionaries

65 pointsby fatiheriklialmost 12 years ago

6 comments

raymondhalmost 12 years ago
Some of these capabilities are already built into Python.<p>See Guido's "dictionary views" described at: <a href="http://www.python.org/dev/peps/pep-3106/" rel="nofollow">http://www.python.org/dev/peps/pep-3106/</a> 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:<p><pre><code> # 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])</code></pre>
评论 #5771726 未加载
lifeisstillgoodalmost 12 years ago
This is one of those odd corners of the python ecosystem<p>I have written at least two dict differs, and I suspect they are like web frameworks - everyone has written a half working one.<p>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.<p>I hope it succeeds - my meware sucks
jmmcdalmost 12 years ago
The list diff is really a set diff, if I understand it right, in that it disregards order and multiple instances? That should only happen if the dict member is a set. Members which are lists should be treated using a Levenshtein-distance style patch. For free you then get patching of string members.<p>I'm getting ambitious now, but what about tree-edit distance (with patch script) for members which are nested lists?<p>There is Python code out there for these:<p><a href="https://github.com/timtadh/zhang-shasha" rel="nofollow">https://github.com/timtadh/zhang-shasha</a><p><a href="https://code.google.com/p/py-editdist" rel="nofollow">https://code.google.com/p/py-editdist</a><p>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.
krazykringlealmost 12 years ago
JSON-patch might also serve: <a href="http://tools.ietf.org/html/rfc6902" rel="nofollow">http://tools.ietf.org/html/rfc6902</a><p>Here's a Python implementation: <a href="https://github.com/stefankoegl/python-json-patch" rel="nofollow">https://github.com/stefankoegl/python-json-patch</a>
SEJeffalmost 12 years ago
This looks like a perfect complement to a mongo or couch data store. Thanks!
Ihmahralmost 12 years ago
What might be some of the use cases?
评论 #5771610 未加载
评论 #5772591 未加载