GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — serialize · sreecodeslayer/deepdiff@b4dcacf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • README.txt
  • deepdiff
    • diff.py
  • docs
    • conf.py
    • index.rst
  • setup.py
  • tests
    • test_serialization.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+22-5Lines changed: 22 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# deepdiff v 3.0.0
1+
# deepdiff v 3.1.0
22

33
<!-- ![Downloads](https://img.shields.io/pypi/dm/deepdiff.svg?style=flat) -->
44
![Python Versions](https://img.shields.io/pypi/pyversions/deepdiff.svg?style=flat)
@@ -202,7 +202,7 @@ Example in DeepDiff for the same operation:
202202

203203
# Views
204204

205-
Starting with DeepDiff v3.0.0, there are two different views into your diffed data: text view (original) and tree view (new).
205+
Starting with DeepDiff v3.1.0, there are two different views into your diffed data: text view (original) and tree view (new).
206206

207207
## Text View
208208

@@ -224,7 +224,7 @@ Example of using the text view.
224224
So for example `ddiff['dictionary_item_removed']` is a set if strings thus this is called the text view.
225225

226226
The following examples are using the *default text view.*
227-
The Tree View is introduced in DeepDiff 3.0.0 and provides traversing capabilities through your diffed data and more!
227+
The Tree View is introduced in DeepDiff 3.1.0 and provides traversing capabilities through your diffed data and more!
228228
Read more about the Tree View at the bottom of this page.
229229

230230

@@ -451,7 +451,7 @@ And if you would like to know the values of items added or removed, please set t
451451

452452
## Tree View
453453

454-
Starting the version 3.0.0 You can choose the view into the deepdiff results.
454+
Starting the version 3.1.0 You can choose the view into the deepdiff results.
455455
The tree view provides you with tree objects that you can traverse through to find the parents of the objects that are diffed and the actual objects that are being diffed.
456456

457457
This view is very useful when dealing with nested objects.
@@ -493,7 +493,7 @@ It gives you the actual objects (t1, t2) throughout the tree of parents and chil
493493

494494
## Examples - Tree View
495495

496-
The Tree View is introduced in DeepDiff 3.0.0.
496+
The Tree View is introduced in DeepDiff 3.1.0.
497497
Set view='tree' in order to use this view.
498498

499499
### Value of an item has changed (Tree View)
@@ -730,6 +730,23 @@ Decimal('1.57')
730730

731731
All the examples for the text view work for the tree view too. You just need to set view='tree' to get it in tree form.
732732

733+
## Serialization
734+
735+
DeepDiff uses jsonpickle in order to serialize and deserialize its results into json. This works for both tree view and text view.
736+
737+
### Serialize and then deserialize back to deepdiff
738+
739+
```py
740+
>>> t1 = {1: 1, 2: 2, 3: 3}
741+
>>> t2 = {1: 1, 2: "2", 3: 3}
742+
>>> ddiff = DeepDiff(t1, t2)
743+
>>> jsoned = ddiff.json
744+
>>> jsoned
745+
'{"type_changes": {"root[2]": {"py/object": "deepdiff.helper.RemapDict", "new_type": {"py/type": "__builtin__.str"}, "new_value": "2", "old_type": {"py/type": "__builtin__.int"}, "old_value": 2}}}'
746+
>>> ddiff_new = DeepDiff.from_json(jsoned)
747+
>>> ddiff == ddiff_new
748+
True
749+
```
733750

734751
## Pycon 2016
735752

Collapse file

‎README.txt‎

Copy file name to clipboardExpand all lines: README.txt
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
**DeepDiff v 3.0.0**
1+
**DeepDiff v 3.1.0**
22

33
Deep Difference of dictionaries, iterables, strings and other objects. It will recursively look for all the changes.
44

@@ -199,8 +199,6 @@ expected is the expected output of the function.
199199
>>> assertEqual(DeepDiff(result, expected), {})
200200

201201

202-
203-
204202
**Difference with Json Patch**
205203

206204
Unlike Json Patch https://tools.ietf.org/html/rfc6902 which is designed only for Json objects, DeepDiff is designed specifically for almost all Python types. In addition to that, DeepDiff checks for type changes and attribute value changes that Json Patch does not cover since there are no such things in Json. Last but not least, DeepDiff gives you the exact path of the item(s) that were changed in Python syntax.
@@ -214,6 +212,22 @@ Example in DeepDiff for the same operation:
214212
>>> DeepDiff(item1, item2)
215213
{'type_changes': {"root['a']['b']['c']": {'old_type': <type 'str'>, 'new_value': 42, 'old_value': 'foo', 'new_type': <type '
216214

215+
**Serialization**
216+
217+
DeepDiff uses jsonpickle in order to serialize and deserialize its results into json. This works for both tree view and text view.
218+
219+
Serialize and then deserialize back to deepdiff:
220+
>>> t1 = {1: 1, 2: 2, 3: 3}
221+
>>> t2 = {1: 1, 2: "2", 3: 3}
222+
>>> ddiff = DeepDiff(t1, t2)
223+
>>> jsoned = ddiff.json
224+
>>> jsoned
225+
'{"type_changes": {"root[2]": {"py/object": "deepdiff.helper.RemapDict", "new_type": {"py/type": "__builtin__.str"}, "new_value": "2", "old_type": {"py/type": "__builtin__.int"}, "old_value": 2}}}'
226+
>>> ddiff_new = DeepDiff.from_json(jsoned)
227+
>>> ddiff == ddiff_new
228+
True
229+
230+
217231
**Pycon 2016**
218232

219233
I was honored to give a talk about how DeepDiff does what it does at Pycon 2016. Please check out the video and let me know what you think:
Collapse file

‎deepdiff/diff.py‎

Copy file name to clipboardExpand all lines: deepdiff/diff.py
+37-4Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import difflib
1414
import logging
15+
import jsonpickle
1516

1617
from decimal import Decimal
1718

@@ -83,7 +84,7 @@ class DeepDiff(ResultDict):
8384
List of object types to exclude from the report.
8485
8586
view: string, default = text
86-
Starting the version 3.0.0 You can choose the view into the deepdiff results.
87+
Starting the version 3.1.0 You can choose the view into the deepdiff results.
8788
The default is the text view which has been the only view up until now.
8889
The new view is called the tree view which allows you to traverse through
8990
the tree of changed items.
@@ -114,7 +115,7 @@ class DeepDiff(ResultDict):
114115
115116
.. seealso::
116117
The following examples are using the *default text view.*
117-
The Tree View is introduced in DeepDiff 3.0.0 and provides traversing capabilities through your diffed data and more!
118+
The Tree View is introduced in DeepDiff 3.1.0 and provides traversing capabilities through your diffed data and more!
118119
Read more about the Tree View at the bottom of this page.
119120
120121
Importing
@@ -328,7 +329,7 @@ class DeepDiff(ResultDict):
328329
329330
**Tree View**
330331
331-
Starting the version 3.0.0 You can choose the view into the deepdiff results.
332+
Starting the version 3.1.0 You can choose the view into the deepdiff results.
332333
The tree view provides you with tree objects that you can traverse through to find
333334
the parents of the objects that are diffed and the actual objects that are being diffed.
334335
This view is very useful when dealing with nested objects.
@@ -373,7 +374,7 @@ class DeepDiff(ResultDict):
373374
**Examples Tree View**
374375
375376
.. note::
376-
The Tree View is introduced in DeepDiff 3.0.0.
377+
The Tree View is introduced in DeepDiff 3.1.0.
377378
Set view='tree' in order to use this view.
378379
379380
Value of an item has changed (Tree View)
@@ -582,6 +583,21 @@ class DeepDiff(ResultDict):
582583
.. note::
583584
All the examples for the text view work for the tree view too. You just need to set view='tree' to get it in tree form.
584585
586+
**Serialization**
587+
588+
DeepDiff uses jsonpickle in order to serialize and deserialize its results into json.
589+
590+
Serialize and then deserialize back to deepdiff
591+
>>> t1 = {1: 1, 2: 2, 3: 3}
592+
>>> t2 = {1: 1, 2: "2", 3: 3}
593+
>>> ddiff = DeepDiff(t1, t2)
594+
>>> jsoned = ddiff.json
595+
>>> jsoned
596+
'{"type_changes": {"root[2]": {"py/object": "deepdiff.helper.RemapDict", "new_type": {"py/type": "__builtin__.str"}, "new_value": "2", "old_type": {"py/type": "__builtin__.int"}, "old_value": 2}}}'
597+
>>> ddiff_new = DeepDiff.from_json(jsoned)
598+
>>> ddiff == ddiff_new
599+
True
600+
585601
**Pycon 2016 Talk**
586602
I gave a talk about how DeepDiff does what it does at Pycon 2016.
587603
`Diff it to Dig it Pycon 2016 video <https://www.youtube.com/watch?v=J5r99eJIxF4>`_
@@ -1076,6 +1092,23 @@ def __diff(self, level, parents_ids=frozenset({})):
10761092

10771093
return
10781094

1095+
@property
1096+
def json(self):
1097+
if not hasattr(self, '_json'):
1098+
# copy of self removes all the extra attributes since it assumes
1099+
# we have only a simple dictionary.
1100+
copied = self.copy()
1101+
self._json = jsonpickle.encode(copied)
1102+
return self._json
1103+
1104+
@json.deleter
1105+
def json(self):
1106+
del self._json
1107+
1108+
@classmethod
1109+
def from_json(self, value):
1110+
return jsonpickle.decode(value)
1111+
10791112

10801113
if __name__ == "__main__": # pragma: no cover
10811114
if not py3:
Collapse file

‎docs/conf.py‎

Copy file name to clipboardExpand all lines: docs/conf.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
# built documents.
6161
#
6262
# The short X.Y version.
63-
version = '3.0.0'
63+
version = '3.1.0'
6464
# The full version, including alpha/beta/rc tags.
65-
release = '3.0.0'
65+
release = '3.1.0'
6666

6767
# The language for content autogenerated by Sphinx. Refer to documentation
6868
# for a list of supported languages.
Collapse file

‎docs/index.rst‎

Copy file name to clipboardExpand all lines: docs/index.rst
+23-3Lines changed: 23 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
DeepDiff 3.0.0 documentation!
6+
DeepDiff 3.1.0 documentation!
77
=============================
88

99
**DeepDiff: Deep Difference of dictionaries, iterables and almost any other object recursively.**
@@ -173,13 +173,13 @@ So for example ddiff['dictionary_item_removed'] is a set if strings thus this is
173173

174174
.. seealso::
175175
The following examples are using the *default text view.*
176-
The Tree View is introduced in DeepDiff 3.0.0 and provides traversing capabilities through your diffed data and more!
176+
The Tree View is introduced in DeepDiff 3.1.0 and provides traversing capabilities through your diffed data and more!
177177
Read more about the Tree View at :doc:`/diff`
178178

179179
Tree View (new)
180180
---------------
181181

182-
Starting the version 3.0.0 You can choose the view into the deepdiff results.
182+
Starting the version 3.1.0 You can choose the view into the deepdiff results.
183183
The tree view provides you with tree objects that you can traverse through to find
184184
the parents of the objects that are diffed and the actual objects that are being diffed.
185185
This view is very useful when dealing with nested objects.
@@ -257,6 +257,26 @@ Verbose level by default is 1. The possible values are 0, 1 and 2.
257257
Read more about the verbosity at :doc:`/diff`
258258

259259

260+
Serialization
261+
~~~~~~~~~~~~~
262+
263+
DeepDiff uses jsonpickle in order to serialize and deserialize its results into json. This works for both tree view and text view.
264+
265+
:Serialize and then deserialize back to deepdiff:
266+
267+
.. code:: python
268+
269+
>>> t1 = {1: 1, 2: 2, 3: 3}
270+
>>> t2 = {1: 1, 2: "2", 3: 3}
271+
>>> ddiff = DeepDiff(t1, t2)
272+
>>> jsoned = ddiff.json
273+
>>> jsoned
274+
'{"type_changes": {"root[2]": {"py/object": "deepdiff.helper.RemapDict", "new_type": {"py/type": "__builtin__.str"}, "new_value": "2", "old_type": {"py/type": "__builtin__.int"}, "old_value": 2}}}'
275+
>>> ddiff_new = DeepDiff.from_json(jsoned)
276+
>>> ddiff == ddiff_new
277+
True
278+
279+
260280
***********
261281
Deep Search
262282
***********
Collapse file

‎setup.py‎

Copy file name to clipboardExpand all lines: setup.py
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
long_description = "Deep Difference and Search of any Python object/data."
1414

1515
setup(name='deepdiff',
16-
version='3.0.0',
16+
version='3.1.0',
1717
description='Deep Difference and Search of any Python object/data.',
1818
url='https://github.com/seperman/deepdiff',
1919
download_url='https://github.com/seperman/deepdiff/tarball/master',
@@ -25,7 +25,10 @@
2525
test_suite="tests",
2626
tests_require=['mock'],
2727
long_description=long_description,
28-
# tests_require=['numpy==1.11.2'], # Disabling this since Numpy does not install on pypy3
28+
tests_require=['numpy==1.11.2'], # Disabling this since Numpy does not install on pypy3
29+
install_requires=[
30+
'jsonpickle'
31+
],
2932
classifiers=[
3033
"Intended Audience :: Developers",
3134
"Operating System :: OS Independent",
Collapse file

‎tests/test_serialization.py‎

Copy file name to clipboard
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
To run only the search tests:
5+
python -m unittest tests.test_serialization
6+
7+
Or to run all the tests:
8+
python -m unittest discover
9+
10+
Or to run all the tests with coverage:
11+
coverage run --source deepdiff setup.py test
12+
13+
Or using Nose:
14+
nosetests --with-coverage --cover-package=deepdiff
15+
16+
To run a specific test, run this from the root of repo:
17+
python -m unittest tests.test_serialization.DeepDiffTextTestCase.test_same_objects
18+
19+
or using nosetests:
20+
nosetests tests/test_serialization.py:DeepDiffTestCase.test_diff_when_hash_fails
21+
"""
22+
import unittest
23+
from deepdiff import DeepDiff
24+
25+
import logging
26+
logging.disable(logging.CRITICAL)
27+
28+
29+
class DeepAdditionsTestCase(unittest.TestCase):
30+
"""Tests for Additions and Subtractions."""
31+
32+
def test_serialization_text(self):
33+
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2, 3]}}
34+
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world\n\n\nEnd"}}
35+
ddiff = DeepDiff(t1, t2)
36+
self.assertTrue("deepdiff.helper.RemapDict" in ddiff.json)
37+
38+
def test_deserialization(self):
39+
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2, 3]}}
40+
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world\n\n\nEnd"}}
41+
ddiff = DeepDiff(t1, t2)
42+
jsoned = ddiff.json
43+
ddiff2 = DeepDiff.from_json(jsoned)
44+
self.assertEqual(ddiff, ddiff2)
45+
46+
def test_serialization_tree(self):
47+
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2, 3]}}
48+
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world\n\n\nEnd"}}
49+
ddiff = DeepDiff(t1, t2, view='tree')
50+
jsoned = ddiff.json
51+
self.assertTrue("world" in jsoned)
52+
53+
def test_deserialization_tree(self):
54+
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2, 3]}}
55+
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world\n\n\nEnd"}}
56+
ddiff = DeepDiff(t1, t2, view='tree')
57+
jsoned = ddiff.json
58+
ddiff2 = DeepDiff.from_json(jsoned)
59+
self.assertTrue('type_changes' in ddiff2)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.