@@ -40,6 +40,9 @@ class DeepDiff(ResultDict): |
40 | 40 | Deep Difference of dictionaries, iterables, strings and almost any other object. |
41 | 41 | It will recursively look for all the changes. |
42 | 42 |
|
| 43 | + DeepDiff 3.0 added the concept of views. |
| 44 | + There is a default "text" view and a "tree" view. |
| 45 | +
|
43 | 46 | **Parameters** |
44 | 47 |
|
45 | 48 | t1 : A dictionary, list, string or any python object that has __dict__ or __slots__ |
@@ -80,6 +83,11 @@ class DeepDiff(ResultDict): |
80 | 83 | exclude_types: list, default = None. |
81 | 84 | List of object types to exclude from the report. |
82 | 85 |
|
| 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. |
83 | 91 |
|
84 | 92 | **Returns** |
85 | 93 |
|
@@ -95,12 +103,11 @@ class DeepDiff(ResultDict): |
95 | 103 |
|
96 | 104 | And here is more info: http://zepworks.com/blog/diff-it-to-digg-it/ |
97 | 105 |
|
98 | | - **Examples** |
| 106 | + **Examples Text View** |
99 | 107 |
|
100 | 108 | Importing |
101 | 109 | >>> from deepdiff import DeepDiff |
102 | 110 | >>> from pprint import pprint |
103 | | - >>> from __future__ import print_function # In case running on Python 2 |
104 | 111 |
|
105 | 112 | Same object returns empty |
106 | 113 | >>> t1 = {1:1, 2:2, 3:3} |
@@ -301,6 +308,191 @@ class DeepDiff(ResultDict): |
301 | 308 | 'root[1]': {'new_value': 1.3362, 'old_value': 1.3359}}} |
302 | 309 | >>> pprint(DeepDiff(1.23*10**20, 1.24*10**20, significant_digits=1)) |
303 | 310 | {'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 | +
|
304 | 496 | """ |
305 | 497 |
|
306 | 498 | show_warning = True |
|