4 ms·
Some of these capabilities are already built into Python. See Guido's "dictionary views" described at: http://www.python.org/dev/peps/pep-3106/ http://www.pyth
by raymondh 13y ago
Some of these capabilities are already built into Python.
See Guido's "dictionary views" described at: http://www.python.org/dev/peps/pep-3106/ 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])
- ot 13y agoWell, this is quite different in the sense that the "patch operations" can also operate on the values, not just add/delete/replace them. In the example, the patch pushes and pops elements from the lists, rather than replacing them altogether. 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.