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 — Adding py3.6, adding docs · sreecodeslayer/deepdiff@f17dfc6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .travis.yml
  • deepdiff
    • diff.py
    • helper.py
    • model.py
  • docs
    • conf.py
  • tests
    • test_model.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.travis.yml‎

Copy file name to clipboardExpand all lines: .travis.yml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ python:
55
- "3.3"
66
- "3.4"
77
- "3.5"
8+
- "3.6"
89
- "pypy-5.4" # pypy on python 2.7
910
- "pypy3"
1011

Collapse file

‎deepdiff/diff.py‎

Copy file name to clipboardExpand all lines: deepdiff/diff.py
+194-2Lines changed: 194 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class DeepDiff(ResultDict):
4040
Deep Difference of dictionaries, iterables, strings and almost any other object.
4141
It will recursively look for all the changes.
4242
43+
DeepDiff 3.0 added the concept of views.
44+
There is a default "text" view and a "tree" view.
45+
4346
**Parameters**
4447
4548
t1 : A dictionary, list, string or any python object that has __dict__ or __slots__
@@ -80,6 +83,11 @@ class DeepDiff(ResultDict):
8083
exclude_types: list, default = None.
8184
List of object types to exclude from the report.
8285
86+
view: string, default = text
87+
Starting the version 3.0.0 You can choose the view into the deepdiff results.
88+
The default is the text view which has been the only view up until now.
89+
The new view is called the tree view which allows you to traverse through
90+
the tree of changed items.
8391
8492
**Returns**
8593
@@ -95,12 +103,11 @@ class DeepDiff(ResultDict):
95103
96104
And here is more info: http://zepworks.com/blog/diff-it-to-digg-it/
97105
98-
**Examples**
106+
**Examples Text View**
99107
100108
Importing
101109
>>> from deepdiff import DeepDiff
102110
>>> from pprint import pprint
103-
>>> from __future__ import print_function # In case running on Python 2
104111
105112
Same object returns empty
106113
>>> t1 = {1:1, 2:2, 3:3}
@@ -301,6 +308,191 @@ class DeepDiff(ResultDict):
301308
'root[1]': {'new_value': 1.3362, 'old_value': 1.3359}}}
302309
>>> pprint(DeepDiff(1.23*10**20, 1.24*10**20, significant_digits=1))
303310
{'values_changed': {'root': {'new_value': 1.24e+20, 'old_value': 1.23e+20}}}
311+
312+
**Examples Tree View**
313+
The tree view provides you with tree objects that you can traverse through to find
314+
the parents of the objects that are diffed and the actual objects that are being diffed.
315+
This view is very useful when dealing with nested objects.
316+
Note that tree view always returns results in the form of Python sets.
317+
We will see through examples how this affects how you retrieve the individual results:
318+
319+
Value of an item has changed.
320+
>>> t1 = {1:1, 2:2, 3:3}
321+
>>> t2 = {1:1, 2:4, 3:3}
322+
>>> ddiff_verbose0 = DeepDiff(t1, t2, verbose_level=0, view='tree')
323+
>>> ddiff_verbose0
324+
{'values_changed': {<root[2]>}}
325+
>>>
326+
>>> ddiff_verbose1 = DeepDiff(t1, t2, verbose_level=1, view='tree')
327+
>>> ddiff_verbose1
328+
{'values_changed': {<root[2] t1:2, t2:4>}}
329+
>>> set_of_values_changed = ddiff_verbose1['values_changed']
330+
>>> # since set_of_values_changed includes only one item in a set
331+
>>> # in order to get that one item we can:
332+
>>> (changed,) = set_of_values_changed
333+
>>> changed # Another way to get this is to do: changed=list(set_of_values_changed)[0]
334+
<root[2] t1:2, t2:4>
335+
>>> changed.t1
336+
2
337+
>>> changed.t2
338+
4
339+
>>> # You can traverse through the tree, get to the parents!
340+
>>> changed.up
341+
<root t1:{1: 1, 2: 2,...}, t2:{1: 1, 2: 4,...}>
342+
343+
List difference
344+
>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3, 4]}}
345+
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2]}}
346+
>>> ddiff = DeepDiff(t1, t2, view='tree')
347+
>>> ddiff
348+
{'iterable_item_removed': {<root[4]['b'][3] t1:4, t2:None>, <root[4]['b'][2] t1:3, t2:None>}}
349+
>>> # Note that the iterable_item_removed is a set. In this case it has 2 items in it.
350+
>>> # One way to get one item from the set is to convert it to a list
351+
>>> # And then get the first item of the list:
352+
>>> removed = list(ddiff['iterable_item_removed'])[0]
353+
>>> removed
354+
<root[4]['b'][2] t1:3, t2:None>
355+
>>>
356+
>>> parent = removed.up
357+
>>> parent
358+
<root[4]['b'] t1:[1, 2, 3, 4], t2:[1, 2]>
359+
>>> parent.path()
360+
"root[4]['b']"
361+
>>> parent.t1
362+
[1, 2, 3, 4]
363+
>>> parent.t2
364+
[1, 2]
365+
>>> parent.up
366+
<root[4] t1:{'a': 'hello...}, t2:{'a': 'hello...}>
367+
>>> parent.up.up
368+
<root t1:{1: 1, 2: 2,...}, t2:{1: 1, 2: 2,...}>
369+
>>> parent.up.up.t1
370+
{1: 1, 2: 2, 3: 3, 4: {'a': 'hello', 'b': [1, 2, 3, 4]}}
371+
>>> parent.up.up.t1 == t1 # It is holding the original t1 that we passed to DeepDiff
372+
True
373+
374+
List difference 2:
375+
>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
376+
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}}
377+
>>> ddiff = DeepDiff(t1, t2, view='tree')
378+
>>> pprint(ddiff, indent = 2)
379+
{ 'iterable_item_added': {<root[4]['b'][3] t1:None, t2:3>},
380+
'values_changed': { <root[4]['b'][1] t1:2, t2:3>,
381+
<root[4]['b'][2] t1:3, t2:2>}}
382+
>>>
383+
>>> # Note that iterable_item_added is a set with one item.
384+
>>> # So in order to get that one item from it, we can do:
385+
>>> (added,) = ddiff['iterable_item_added']
386+
>>> added
387+
<root[4]['b'][3] t1:None, t2:3>
388+
>>> added.up.up
389+
<root[4] t1:{'a': 'hello...}, t2:{'a': 'hello...}>
390+
>>> added.up.up.path()
391+
'root[4]'
392+
>>> added.up.up.down
393+
<root[4]['b'] t1:[1, 2, 3], t2:[1, 3, 2, 3]>
394+
# going up twice and then down twice gives you the same node in the tree:
395+
>>> added.up.up.down.down == added
396+
True
397+
398+
List difference ignoring order but reporting repetitions:
399+
>>> t1 = [1, 3, 1, 4]
400+
>>> t2 = [4, 4, 1]
401+
>>> ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, view='tree')
402+
>>> pprint(ddiff, indent=2)
403+
{ 'iterable_item_removed': {<root[1] t1:3, t2:None>},
404+
'repetition_change': { <root[3] {'repetition': {'old_repeat': 1,...}>,
405+
<root[0] {'repetition': {'old_repeat': 2,...}>}}
406+
>>>
407+
>>> # repetition_change is a set with 2 items.
408+
>>> # in order to get those 2 items, we can do the following.
409+
>>> # or we can convert the set to list and get the list items.
410+
>>> # or we can iterate through the set items
411+
>>> (repeat1, repeat2) = ddiff['repetition_change']
412+
>>> repeat1 # the default verbosity is set to 1.
413+
<root[0] {'repetition': {'old_repeat': 2,...}>
414+
>>> # The actual data regarding the repetitions can be found in the repetition attribute:
415+
>>> repeat1.repetition
416+
{'old_repeat': 1, 'new_repeat': 2, 'old_indexes': [3], 'new_indexes': [0, 1]}
417+
>>>
418+
>>> # If you change the verbosity, you will see less:
419+
>>> ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, view='tree', verbose_level=0)
420+
>>> ddiff
421+
{'repetition_change': {<root[3]>, <root[0]>}, 'iterable_item_removed': {<root[1]>}}
422+
>>> (repeat1, repeat2) = ddiff['repetition_change']
423+
>>> repeat1
424+
<root[0]>
425+
>>> # But the verbosity level does not change the actual report object:
426+
>>> repeat1.repetition
427+
{'old_repeat': 1, 'new_repeat': 2, 'old_indexes': [3], 'new_indexes': [0, 1]}
428+
>>> repeat1.t1
429+
4
430+
>>> repeat1.t2
431+
4
432+
>>> repeat1.up
433+
<root>
434+
435+
List that contains dictionary:
436+
>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:1, 2:2}]}}
437+
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:3}]}}
438+
>>> ddiff = DeepDiff(t1, t2)
439+
>>> pprint (ddiff, indent = 2)
440+
{ 'dictionary_item_removed': {"root[4]['b'][2][2]"},
441+
'values_changed': {"root[4]['b'][2][1]": {'new_value': 3, 'old_value': 1}}}
442+
443+
Sets:
444+
>>> t1 = {1, 2, 8}
445+
>>> t2 = {1, 2, 3, 5}
446+
>>> ddiff = DeepDiff(t1, t2)
447+
>>> pprint (DeepDiff(t1, t2))
448+
{'set_item_added': {'root[5]', 'root[3]'}, 'set_item_removed': {'root[8]'}}
449+
450+
Named Tuples:
451+
>>> from collections import namedtuple
452+
>>> Point = namedtuple('Point', ['x', 'y'])
453+
>>> t1 = Point(x=11, y=22)
454+
>>> t2 = Point(x=11, y=23)
455+
>>> pprint (DeepDiff(t1, t2))
456+
{'values_changed': {'root.y': {'new_value': 23, 'old_value': 22}}}
457+
458+
Custom objects:
459+
>>> class ClassA(object):
460+
... a = 1
461+
... def __init__(self, b):
462+
... self.b = b
463+
...
464+
>>> t1 = ClassA(1)
465+
>>> t2 = ClassA(2)
466+
>>>
467+
>>> pprint(DeepDiff(t1, t2))
468+
{'values_changed': {'root.b': {'new_value': 2, 'old_value': 1}}}
469+
470+
Object attribute added:
471+
>>> t2.c = "new attribute"
472+
>>> pprint(DeepDiff(t1, t2))
473+
{'attribute_added': {'root.c'},
474+
'values_changed': {'root.b': {'new_value': 2, 'old_value': 1}}}
475+
476+
Approximate decimals comparison (Significant digits after the point):
477+
>>> t1 = Decimal('1.52')
478+
>>> t2 = Decimal('1.57')
479+
>>> DeepDiff(t1, t2, significant_digits=0)
480+
{}
481+
>>> DeepDiff(t1, t2, significant_digits=1)
482+
{'values_changed': {'root': {'old_value': Decimal('1.52'), 'new_value': Decimal('1.57')}}}
483+
484+
Approximate float comparison (Significant digits after the point):
485+
>>> t1 = [ 1.1129, 1.3359 ]
486+
>>> t2 = [ 1.113, 1.3362 ]
487+
>>> pprint(DeepDiff(t1, t2, significant_digits=3))
488+
{}
489+
>>> pprint(DeepDiff(t1, t2))
490+
{'values_changed': {'root[0]': {'new_value': 1.113, 'old_value': 1.1129},
491+
'root[1]': {'new_value': 1.3362, 'old_value': 1.3359}}}
492+
>>> pprint(DeepDiff(1.23*10**20, 1.24*10**20, significant_digits=1))
493+
{'values_changed': {'root': {'new_value': 1.24e+20, 'old_value': 1.23e+20}}}
494+
495+
304496
"""
305497

306498
show_warning = True
Collapse file

‎deepdiff/helper.py‎

Copy file name to clipboardExpand all lines: deepdiff/helper.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def short_repr(item, max_length=15):
4949
"""Short representation of item if it is too long"""
5050
item = repr(item)
5151
if len(item) > max_length:
52-
item = '{}...{}'.format(item[:12], item[-1])
52+
item = '{}...{}'.format(item[:max_length - 3], item[-1])
5353
return item
5454

5555

Collapse file

‎deepdiff/model.py‎

Copy file name to clipboardExpand all lines: deepdiff/model.py
+15-4Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
FORCE_DEFAULT = 'fake'
99
UP_DOWN = {'up': 'down', 'down': 'up'}
1010

11-
1211
REPORT_KEYS = {
1312
"type_changes",
1413
"dictionary_item_added",
@@ -25,6 +24,10 @@
2524
}
2625

2726

27+
class DoesNotExist(Exception):
28+
pass
29+
30+
2831
class ResultDict(RemapDict):
2932
def cleanup(self):
3033
"""
@@ -315,9 +318,13 @@ def __init__(self,
315318

316319
def __repr__(self):
317320
if Verbose.level:
318-
t1_repr = short_repr(self.t1)
319-
t2_repr = short_repr(self.t2)
320-
result = "<{} t1:{}, t2:{}>".format(self.path(), t1_repr, t2_repr)
321+
if self.additional:
322+
additional_repr = short_repr(self.additional, max_length=35)
323+
result = "<{} {}>".format(self.path(), additional_repr)
324+
else:
325+
t1_repr = short_repr(self.t1)
326+
t2_repr = short_repr(self.t2)
327+
result = "<{} t1:{}, t2:{}>".format(self.path(), t1_repr, t2_repr)
321328
else:
322329
result = "<{}>".format(self.path())
323330
return result
@@ -331,6 +338,10 @@ def __setattr__(self, key, value):
331338
else:
332339
self.__dict__[key] = value
333340

341+
@property
342+
def repetition(self):
343+
return self.additional['repetition']
344+
334345
def auto_generate_child_rel(self, klass, param):
335346
"""
336347
Auto-populate self.child_rel1 and self.child_rel2.
Collapse file

‎docs/conf.py‎

Copy file name to clipboardExpand all lines: docs/conf.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@
5151

5252
# General information about the project.
5353
project = 'DeepDiff'
54-
copyright = '2015-2016, Sep Ehr (Seperman)'
55-
author = 'Sep Dehpour (Seperman)'
54+
copyright = '2015-2017, Sep Dehpour'
55+
author = 'Sep Dehpour'
5656

5757
# The version info for the project you're documenting, acts as replacement for
5858
# |version| and |release|, also used in various other places throughout the
5959
# built documents.
6060
#
6161
# The short X.Y version.
62-
version = '1.6.0'
62+
version = '3.0.0'
6363
# The full version, including alpha/beta/rc tags.
64-
release = '1.6.0'
64+
release = '3.0.0'
6565

6666
# The language for content autogenerated by Sphinx. Refer to documentation
6767
# for a list of supported languages.
@@ -227,7 +227,7 @@
227227
# author, documentclass [howto, manual, or own class]).
228228
latex_documents = [
229229
(master_doc, 'DeepDiff.tex', 'DeepDiff Documentation',
230-
'Sep Ehr (Seperman)', 'manual'),
230+
'Sep Dehpour', 'manual'),
231231
]
232232

233233
# The name of an image file (relative to this directory) to place at the top of
Collapse file

‎tests/test_model.py‎

Copy file name to clipboardExpand all lines: tests/test_model.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ def test_repr_long(self):
211211
self.assertEqual(item_repr,
212212
"<root[1337].a t1:'very long t...', t2:313>")
213213

214+
def test_repetition_attribute(self):
215+
t1 = [1, 1]
216+
t2 = [1]
217+
some_repetition = 'some repetition'
218+
node = DiffLevel(t1, t2)
219+
node.additional['repetition'] = some_repetition
220+
self.assertEqual(node.repetition, some_repetition)
221+
214222

215223
class ChildRelationshipTestCase(TestCase):
216224
def test_create_invalid_klass(self):

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.