Skip to content

Commit

Permalink
Add utility for stripping DiffEntry class from diff objects, left wit…
Browse files Browse the repository at this point in the history
…h only pure python dict objects without attribute access, and test conversion to/from json with this.
  • Loading branch information
Martin Sandve Alnæs committed Jan 29, 2016
1 parent 7505bcd commit 003da65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nbdime/diff_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ def source_as_string(source):
return source


def to_clean_dicts(di):
"Recursively convert dict-like objects to straight python dicts."
if isinstance(di, dict):
return {k: to_clean_dicts(v) for k, v in di.items()}
elif isinstance(di, list):
return [to_clean_dicts(v) for v in di]
else:
return di


def to_json_patch(d, path=""):
"""Convert nbdime diff object into the RFC6902 JSON Patch format.
Expand Down
19 changes: 19 additions & 0 deletions nbdime/tests/test_diff_json_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import json
from nbdime import diff
from nbdime.diff_format import to_clean_dicts

def test_diff_to_json():
a = { "foo": [1,2,3], "bar": {"ting": 7, "tang": 123 } }
b = { "foo": [1,3,4], "bar": {"tang": 126, "hello": "world" } }
d1 = diff(a, b)

d2 = to_clean_dicts(d1)
assert len(d2) == len(d1)
assert all(len(e2) == len(e1) for e1, e2 in zip(d1, d2))

j = json.dumps(d1)
d3 = json.loads(j)
assert len(d3) == len(d1)
assert all(len(e3) == len(e1) for e1, e3 in zip(d1, d3))
assert d2 == d3

0 comments on commit 003da65

Please sign in to comment.