Debian Codegraph @

docs/view.rst

raw
1 :doc:`/index`
2
3 .. _view_label:
4
5 View
6 ====
7
8 You have the options of text view and tree view.
9 The main difference is that the tree view has the capabilities to traverse the objects to see what objects were compared to what other objects.
10
11 While the view options decide the format of the output that is mostly machine readable, regardless of the view you choose, you can get a more human readable output by using the pretty() method.
12 DeepDiff also offers other specialized views such as the :doc:`colored_view` (which includes a compact variant) and :doc:`delta` view for specific use cases.
13
14 .. _text_view_label:
15
16 Text View
17 ---------
18
19 Text view is the default view of DeepDiff. It is simpler than tree view.
20
21 Example of using the text view.
22 >>> from decimal import Decimal
23 >>> from deepdiff import DeepDiff
24 >>> t1 = {1:1, 3:3, 4:4}
25 >>> t2 = {1:1, 3:3, 5:5, 6:6}
26 >>> ddiff = DeepDiff(t1, t2)
27 >>> print(ddiff)
28 {'dictionary_item_added': [root[5], root[6]], 'dictionary_item_removed': [root[4]]}
29
30 So for example ddiff['dictionary_item_added'] is a set of string results. That's why this view is called the text view.
31 You can get this view by default or by passing `view='text'`.
32
33 .. _tree_view_label:
34
35 Tree View
36 ---------
37
38 The tree view provides you with tree objects that you can traverse through to find
39 the parents of the objects that are diffed and the actual objects that are being diffed.
40 This view is very useful when dealing with nested objects.
41 Note that tree view always returns results in the form of Python sets.
42
43 You can traverse through the tree elements!
44
45 .. note::
46 The Tree view is just a different representation of the diffed data.
47 Behind the scene, DeepDiff creates the tree view first and then converts it to textual
48 representation for the text view.
49
50 **Tree View Interface**
51
52 .. code:: text
53
54 +---------------------------------------------------------------+
55 | |
56 | parent(t1) parent node parent(t2) |----level
57 | + ^ + |
58 +------|--------------------------|---------------------|-------+
59 | | | up |
60 | Child | | | ChildRelationship
61 | Relationship | | |
62 | down | | |
63 +------|----------------------|-------------------------|-------+
64 | v v v |
65 | child(t1) child node child(t2) |----level
66 | |
67 +---------------------------------------------------------------+
68
69
70 :up: Move up to the parent node aka parent level
71 :down: Move down to the child node aka child level
72 :path(): Get the path to the current node in string representation, path(output_format='list') gives you the path in list representation. path(use_t2=True) gives you the path to t2.
73 :t1: The first item in the current node that is being diffed
74 :t2: The second item in the current node that is being diffed
75 :additional: Additional information about the node i.e. repetition
76 :repetition: Shortcut to get the repetition report
77
78
79 The tree view allows you to have more than mere textual representaion of the diffed objects.
80 It gives you the actual objects (t1, t2) throughout the tree of parents and children.
81
82 **Examples for Tree View**
83
84 .. note::
85 Set view='tree' in order to get the results in tree view.
86
87 Value of an item has changed (Tree View)
88 >>> from deepdiff import DeepDiff
89 >>> from pprint import pprint
90 >>> t1 = {1:1, 2:2, 3:3}
91 >>> t2 = {1:1, 2:4, 3:3}
92 >>> ddiff_verbose0 = DeepDiff(t1, t2, verbose_level=0, view='tree')
93 >>> ddiff_verbose0
94 {'values_changed': [<root[2]>]}
95 >>>
96 >>> ddiff_verbose1 = DeepDiff(t1, t2, verbose_level=1, view='tree')
97 >>> ddiff_verbose1
98 {'values_changed': [<root[2] t1:2, t2:4>]}
99 >>> set_of_values_changed = ddiff_verbose1['values_changed']
100 >>> # since set_of_values_changed includes only one item in a set
101 >>> # in order to get that one item we can:
102 >>> (changed,) = set_of_values_changed
103 >>> changed # Another way to get this is to do: changed=list(set_of_values_changed)[0]
104 <root[2] t1:2, t2:4>
105 >>> changed.t1
106 2
107 >>> changed.t2
108 4
109 >>> # You can traverse through the tree, get to the parents!
110 >>> changed.up
111 <root t1:{1: 1, 2: 2,...}, t2:{1: 1, 2: 4,...}>
112
113 List difference (Tree View)
114 >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3, 4]}}
115 >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2]}}
116 >>> ddiff = DeepDiff(t1, t2, view='tree')
117 >>> ddiff
118 {'iterable_item_removed': [<root[4]['b'][2] t1:3, t2:not present>, <root[4]['b'][3] t1:4, t2:not present>]}
119 >>> # Note that the iterable_item_removed is a set. In this case it has 2 items in it.
120 >>> # One way to get one item from the set is to convert it to a list
121 >>> # And then get the first item of the list:
122 >>> removed = list(ddiff['iterable_item_removed'])[0]
123 >>> removed
124 <root[4]['b'][2] t1:3, t2:not present>
125 >>>
126 >>> parent = removed.up
127 >>> parent
128 <root[4]['b'] t1:[1, 2, 3, 4], t2:[1, 2]>
129 >>> parent.path() # gives you the string representation of the path
130 "root[4]['b']"
131 >>> parent.path(output_format='list') # gives you the list of keys and attributes that make up the path
132 [4, 'b']
133 >>> parent.t1
134 [1, 2, 3, 4]
135 >>> parent.t2
136 [1, 2]
137 >>> parent.up
138 <root[4] t1:{'a': 'hello...}, t2:{'a': 'hello...}>
139 >>> parent.up.up
140 <root t1:{1: 1, 2: 2,...}, t2:{1: 1, 2: 2,...}>
141 >>> parent.up.up.t1
142 {1: 1, 2: 2, 3: 3, 4: {'a': 'hello', 'b': [1, 2, 3, 4]}}
143 >>> parent.up.up.t1 == t1 # It is holding the original t1 that we passed to DeepDiff
144 True
145
146 List difference 2 (Tree View)
147 >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
148 >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}}
149 >>> ddiff = DeepDiff(t1, t2, view='tree')
150 >>> pprint(ddiff, indent = 2)
151 { 'iterable_item_added': [<root[4]['b'][3] t1:not present, t2:3>],
152 'values_changed': [<root[4]['b'][1] t1:2, t2:3>, <root[4]['b'][2] t1:3, t2:2>]}
153 >>>
154 >>> # Note that iterable_item_added is a set with one item.
155 >>> # So in order to get that one item from it, we can do:
156 >>>
157 >>> (added,) = ddiff['iterable_item_added']
158 >>> added
159 <root[4]['b'][3] t1:not present, t2:3>
160 >>> added.up.up
161 <root[4] t1:{'a': 'hello...}, t2:{'a': 'hello...}>
162 >>> added.up.up.path()
163 'root[4]'
164 >>> added.up.up.path(output_format='list') # gives you the list of keys and attributes that make up the path
165 [4]
166 >>> added.up.up.down
167 <root[4]['b'] t1:[1, 2, 3], t2:[1, 3, 2, 3]>
168 >>>
169 >>> # going up twice and then down twice gives you the same node in the tree:
170 >>> added.up.up.down.down == added
171 True
172
173 List difference ignoring order but reporting repetitions (Tree View)
174 >>> t1 = [1, 3, 1, 4]
175 >>> t2 = [4, 4, 1]
176 >>> ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, view='tree')
177 >>> pprint(ddiff, indent=2)
178 { 'iterable_item_removed': [<root[1] t1:3, t2:not present>],
179 'repetition_change': [<root[3] {'repetition': {'old_repeat': 1,...}>, <root[0] {'repetition': {'old_repeat': 2,...}>]}
180 >>>
181 >>> # repetition_change is a set with 2 items.
182 >>> # in order to get those 2 items, we can do the following.
183 >>> # or we can convert the set to list and get the list items.
184 >>> # or we can iterate through the set items
185 >>>
186 >>> (repeat1, repeat2) = ddiff['repetition_change']
187 >>> repeat1 # the default verbosity is set to 1.
188 <root[3] {'repetition': {'old_repeat': 1,...}>
189 >>> # The actual data regarding the repetitions can be found in the repetition attribute:
190 >>> repeat1.repetition
191 {'old_repeat': 1, 'new_repeat': 2, 'old_indexes': [3], 'new_indexes': [0, 1]}
192 >>>
193 >>> # If you change the verbosity, you will see less:
194 >>> ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True, view='tree', verbose_level=0)
195 >>> ddiff
196 {'repetition_change': [<root[3]>, <root[0]>], 'iterable_item_removed': [<root[1]>]}
197 >>> (repeat1, repeat2) = ddiff['repetition_change']
198 >>> repeat1
199 <root[0]>
200 >>>
201 >>> # But the verbosity level does not change the actual report object.
202 >>> # It only changes the textual representaion of the object. We get the actual object here:
203 >>> repeat1.repetition
204 {'old_repeat': 1, 'new_repeat': 2, 'old_indexes': [3], 'new_indexes': [0, 1]}
205 >>> repeat1.t1
206 4
207 >>> repeat1.t2
208 4
209 >>> repeat1.up
210 <root>
211
212 List that contains dictionary (Tree View)
213 >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:1, 2:2}]}}
214 >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:3}]}}
215 >>> ddiff = DeepDiff(t1, t2, view='tree')
216 >>> pprint (ddiff, indent = 2)
217 { 'dictionary_item_removed': [<root[4]['b'][2][2] t1:2, t2:not present>],
218 'values_changed': [<root[4]['b'][2][1] t1:1, t2:3>]}
219
220 Sets (Tree View):
221 >>> t1 = {1, 2, 8}
222 >>> t2 = {1, 2, 3, 5}
223 >>> ddiff = DeepDiff(t1, t2, view='tree')
224 >>> print(ddiff)
225 {'set_item_removed': [<root: t1:8, t2:not present>], 'set_item_added': [<root: t1:not present, t2:3>, <root: t1:not present, t2:5>]}
226 >>> # grabbing one item from set_item_removed set which has one item only
227 >>> (item,) = ddiff['set_item_removed']
228 >>> item.up
229 <root t1:{8, 1, 2}, t2:{1, 2, 3, 5}>
230 >>> item.up.t1 == t1
231 True
232
233 Named Tuples (Tree View):
234 >>> from collections import namedtuple
235 >>> Point = namedtuple('Point', ['x', 'y'])
236 >>> t1 = Point(x=11, y=22)
237 >>> t2 = Point(x=11, y=23)
238 >>> print(DeepDiff(t1, t2, view='tree'))
239 {'values_changed': [<root.y t1:22, t2:23>]}
240
241 Custom objects (Tree View):
242 >>> class ClassA(object):
243 ... a = 1
244 ... def __init__(self, b):
245 ... self.b = b
246 ...
247 >>> t1 = ClassA(1)
248 >>> t2 = ClassA(2)
249 >>>
250 >>> print(DeepDiff(t1, t2, view='tree'))
251 {'values_changed': [<root.b t1:1, t2:2>]}
252
253 Object attribute added (Tree View):
254 >>> t2.c = "new attribute"
255 >>> pprint(DeepDiff(t1, t2, view='tree'))
256 {'attribute_added': [<root.c t1:not present, t2:'new attribute'>],
257 'values_changed': [<root.b t1:1, t2:2>]}
258
259 Approximate decimals comparison (Significant digits after the point) (Tree View):
260 >>> t1 = Decimal('1.52')
261 >>> t2 = Decimal('1.57')
262 >>> DeepDiff(t1, t2, significant_digits=0, view='tree')
263 {}
264 >>> ddiff = DeepDiff(t1, t2, significant_digits=1, view='tree')
265 >>> ddiff
266 {'values_changed': [<root t1:Decimal('1.52'), t2:Decimal('1.57')>]}
267 >>> (change1,) = ddiff['values_changed']
268 >>> change1
269 <root t1:Decimal('1.52'), t2:Decimal('1.57')>
270 >>> change1.t1
271 Decimal('1.52')
272 >>> change1.t2
273 Decimal('1.57')
274 >>> change1.path()
275 'root'
276
277 Approximate float comparison (Significant digits after the point) (Tree View):
278 >>> t1 = [ 1.1129, 1.3359 ]
279 >>> t2 = [ 1.113, 1.3362 ]
280 >>> ddiff = DeepDiff(t1, t2, significant_digits=3, view='tree')
281 >>> ddiff
282 {}
283 >>> ddiff = DeepDiff(t1, t2, view='tree')
284 >>> pprint(ddiff, indent=2)
285 { 'values_changed': [<root[0] t1:1.1129, t2:1.113>, <root[1] t1:1.3359, t2:1.3362>]}
286 >>> ddiff = DeepDiff(1.23*10**20, 1.24*10**20, significant_digits=1, view='tree')
287 >>> ddiff
288 {'values_changed': [<root t1:1.23e+20, t2:1.24e+20>]}
289
290
291 pretty() method
292 ---------------
293
294 Use the pretty method for human readable output. This is regardless of what view you have used to generate the results.
295 >>> from deepdiff import DeepDiff
296 >>> t1={1,2,4}
297 >>> t2={2,3}
298 >>> print(DeepDiff(t1, t2).pretty())
299 Item root[3] added to set.
300 Item root[4] removed from set.
301 Item root[1] removed from set.
302
303 The pretty method has an optional parameter ``prefix`` that allows a prefix string before every output line (*e.g.* for logging):
304 >>> from deepdiff import DeepDiff
305 >>> t1={1,2,4}
306 >>> t2={2,3}
307 >>> print(DeepDiff(t1, t2).pretty(prefix='Diff: '))
308 Diff: Item root[3] added to set.
309 Diff: Item root[4] removed from set.
310 Diff: Item root[1] removed from set.
311
312 The ``prefix`` may also be a callable function. This function must accept ``**kwargs``; as of this version, the only parameter is ``diff`` but the signature allows for future expansion.
313 The ``diff`` given will be the ``DeepDiff`` that ``pretty`` was called on; this allows interesting capabilities such as:
314
315 .. code:: python
316
317 >>> from deepdiff import DeepDiff
318 >>> t1={1,2,4}
319 >>> t2={2,3}
320 >>> def callback(**kwargs):
321 ... """Helper function using a hidden variable on the diff that tracks which count prints next"""
322 ... kwargs['diff']._diff_count = 1 + getattr(kwargs['diff'], '_diff_count', 0)
323 ... return f"Diff #{kwargs['diff']._diff_count}: "
324 ...
325 >>> print(DeepDiff(t1, t2).pretty(prefix=callback))
326 Diff #1: Item root[3] added to set.
327 Diff #2: Item root[4] removed from set.
328 Diff #3: Item root[1] removed from set.
329
330
331 Text view vs. Tree view vs. pretty() method
332 -----------------------------------------------
333
334 Views are just different format of results. Each comes with its own set of features. At the end of the day the user can choose the right format based on the use case.
335
336 - The text view is the default format of the results. It is the format that is the most suitable if you don't need to know the traversal history of the objects being compared.
337 - The tree view allows you to traverse back and forth through the tree and see what objects were compared to what other objects.
338 - The pretty() method is not a view. All the views are dictionaries. The pretty() method spits out a string output of what has changed and is designed to be human readable.
339
340 For example
341 >>> from deepdiff import DeepDiff
342 >>> t1={1,2,4}
343 >>> t2={2,3}
344
345 Text view (default)
346 >>> DeepDiff(t1, t2) # same as view='text'
347 {'set_item_removed': [root[4], root[1]], 'set_item_added': [root[3]]}
348
349 Tree view
350 >>> tree = DeepDiff(t1, t2, view='tree')
351 >>> tree
352 {'set_item_removed': [<root: t1:4, t2:not present>, <root: t1:1, t2:not present>], 'set_item_added': [<root: t1:not present, t2:3>]}
353 >>> tree['set_item_added'][0]
354 <root: t1:not present, t2:3>
355 >>> tree['set_item_added'][0].t2
356 3
357
358 Pretty method. Regardless of what view was used, you can use the "pretty()" method to get a human readable output.
359 >>> print(DeepDiff(t1, t2).pretty())
360 Item root[3] added to set.
361 Item root[4] removed from set.
362 Item root[1] removed from set.
363
364
365 Back to :doc:`/index`