GeezerVison is showing github.com — serialize · sreecodeslayer/deepdiff@b4dcacf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
| 1 | | -# deepdiff v 3.0.0 | | 1 | +# deepdiff v 3.1.0 | 2 | 2 |
| 3 | 3 | <!--  --> | 4 | 4 |  | @@ -202,7 +202,7 @@ Example in DeepDiff for the same operation: | 202 | 202 |
| 203 | 203 | # Views | 204 | 204 |
| 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). | 206 | 206 |
| 207 | 207 | ## Text View | 208 | 208 |
| @@ -224,7 +224,7 @@ Example of using the text view. | 224 | 224 | So for example `ddiff['dictionary_item_removed']` is a set if strings thus this is called the text view. | 225 | 225 |
| 226 | 226 | 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! | 228 | 228 | Read more about the Tree View at the bottom of this page. | 229 | 229 |
| 230 | 230 |
| @@ -451,7 +451,7 @@ And if you would like to know the values of items added or removed, please set t | 451 | 451 |
| 452 | 452 | ## Tree View | 453 | 453 |
| 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. | 455 | 455 | 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. | 456 | 456 |
| 457 | 457 | 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 | 493 | 493 |
| 494 | 494 | ## Examples - Tree View | 495 | 495 |
| 496 | | - The Tree View is introduced in DeepDiff 3.0.0. | | 496 | + The Tree View is introduced in DeepDiff 3.1.0. | 497 | 497 | Set view='tree' in order to use this view. | 498 | 498 |
| 499 | 499 | ### Value of an item has changed (Tree View) | @@ -730,6 +730,23 @@ Decimal('1.57') | 730 | 730 |
| 731 | 731 | 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. | 732 | 732 |
| | 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 | +``` | 733 | 750 |
| 734 | 751 | ## Pycon 2016 | 735 | 752 |
| |
Collapse file Copy file name to clipboardExpand all lines: README.txt +17-3Lines changed: 17 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -**DeepDiff v 3.0.0** | | 1 | +**DeepDiff v 3.1.0** | 2 | 2 |
| 3 | 3 | Deep Difference of dictionaries, iterables, strings and other objects. It will recursively look for all the changes. | 4 | 4 |
| @@ -199,8 +199,6 @@ expected is the expected output of the function. | 199 | 199 | >>> assertEqual(DeepDiff(result, expected), {}) | 200 | 200 |
| 201 | 201 |
| 202 | | - | 203 | | - | 204 | 202 | **Difference with Json Patch** | 205 | 203 |
| 206 | 204 | 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: | 214 | 212 | >>> DeepDiff(item1, item2) | 215 | 213 | {'type_changes': {"root['a']['b']['c']": {'old_type': <type 'str'>, 'new_value': 42, 'old_value': 'foo', 'new_type': <type ' | 216 | 214 |
| | 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 | + | 217 | 231 | **Pycon 2016** | 218 | 232 |
| 219 | 233 | 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 Copy file name to clipboardExpand all lines: deepdiff/diff.py +37-4Lines changed: 37 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 |
| 13 | 13 | import difflib | 14 | 14 | import logging | | 15 | +import jsonpickle | 15 | 16 |
| 16 | 17 | from decimal import Decimal | 17 | 18 |
| @@ -83,7 +84,7 @@ class DeepDiff(ResultDict): | 83 | 84 | List of object types to exclude from the report. | 84 | 85 |
| 85 | 86 | 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. | 87 | 88 | The default is the text view which has been the only view up until now. | 88 | 89 | The new view is called the tree view which allows you to traverse through | 89 | 90 | the tree of changed items. | @@ -114,7 +115,7 @@ class DeepDiff(ResultDict): | 114 | 115 |
| 115 | 116 | .. seealso:: | 116 | 117 | 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! | 118 | 119 | Read more about the Tree View at the bottom of this page. | 119 | 120 |
| 120 | 121 | Importing | @@ -328,7 +329,7 @@ class DeepDiff(ResultDict): | 328 | 329 |
| 329 | 330 | **Tree View** | 330 | 331 |
| 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. | 332 | 333 | The tree view provides you with tree objects that you can traverse through to find | 333 | 334 | the parents of the objects that are diffed and the actual objects that are being diffed. | 334 | 335 | This view is very useful when dealing with nested objects. | @@ -373,7 +374,7 @@ class DeepDiff(ResultDict): | 373 | 374 | **Examples Tree View** | 374 | 375 |
| 375 | 376 | .. note:: | 376 | | - The Tree View is introduced in DeepDiff 3.0.0. | | 377 | + The Tree View is introduced in DeepDiff 3.1.0. | 377 | 378 | Set view='tree' in order to use this view. | 378 | 379 |
| 379 | 380 | Value of an item has changed (Tree View) | @@ -582,6 +583,21 @@ class DeepDiff(ResultDict): | 582 | 583 | .. note:: | 583 | 584 | 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. | 584 | 585 |
| | 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 | +
| 585 | 601 | **Pycon 2016 Talk** | 586 | 602 | I gave a talk about how DeepDiff does what it does at Pycon 2016. | 587 | 603 | `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({})): | 1076 | 1092 |
| 1077 | 1093 | return | 1078 | 1094 |
| | 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 | + | 1079 | 1112 |
| 1080 | 1113 | if __name__ == "__main__": # pragma: no cover | 1081 | 1114 | if not py3: | |
Collapse file Copy file name to clipboardExpand all lines: docs/conf.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 60 | 60 | # built documents. | 61 | 61 | # | 62 | 62 | # The short X.Y version. | 63 | | -version = '3.0.0' | | 63 | +version = '3.1.0' | 64 | 64 | # The full version, including alpha/beta/rc tags. | 65 | | -release = '3.0.0' | | 65 | +release = '3.1.0' | 66 | 66 |
| 67 | 67 | # The language for content autogenerated by Sphinx. Refer to documentation | 68 | 68 | # for a list of supported languages. | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 3 | 3 | You can adapt this file completely to your liking, but it should at least | 4 | 4 | contain the root `toctree` directive. | 5 | 5 |
| 6 | | -DeepDiff 3.0.0 documentation! | | 6 | +DeepDiff 3.1.0 documentation! | 7 | 7 | ============================= | 8 | 8 |
| 9 | 9 | **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 | 173 | 173 |
| 174 | 174 | .. seealso:: | 175 | 175 | 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! | 177 | 177 | Read more about the Tree View at :doc:`/diff` | 178 | 178 |
| 179 | 179 | Tree View (new) | 180 | 180 | --------------- | 181 | 181 |
| 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. | 183 | 183 | The tree view provides you with tree objects that you can traverse through to find | 184 | 184 | the parents of the objects that are diffed and the actual objects that are being diffed. | 185 | 185 | 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. | 257 | 257 | Read more about the verbosity at :doc:`/diff` | 258 | 258 |
| 259 | 259 |
| | 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 | +
| 260 | 280 | *********** | 261 | 281 | Deep Search | 262 | 282 | *********** | |
Collapse file Copy file name to clipboardExpand all lines: setup.py +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | long_description = "Deep Difference and Search of any Python object/data." | 14 | 14 |
| 15 | 15 | setup(name='deepdiff', | 16 | | - version='3.0.0', | | 16 | + version='3.1.0', | 17 | 17 | description='Deep Difference and Search of any Python object/data.', | 18 | 18 | url='https://github.com/seperman/deepdiff', | 19 | 19 | download_url='https://github.com/seperman/deepdiff/tarball/master', | | 25 | 25 | test_suite="tests", | 26 | 26 | tests_require=['mock'], | 27 | 27 | 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 | + ], | 29 | 32 | classifiers=[ | 30 | 33 | "Intended Audience :: Developers", | 31 | 34 | "Operating System :: OS Independent", | |
Collapse file Copy file name to clipboard +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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) |
|