@@ -35,20 +35,9 @@ Importing
3535 >> > from deepdiff import DeepHash # For hashing objects based on their contents
3636
3737********
38- Features
38+ DeepDiff
3939********
4040
41- Parameters
42- ~~~~~~~~~~
43-
44- - t1 (the first object)
45- - t2 (the second object)
46- - `ignore\_ order `_
47- - `report\_ repetition `_
48- - `exclude\_ types\_ or\_ paths `_
49- - `significant\_ digits `_
50- - `views `_
51-
5241Supported data types
5342~~~~~~~~~~~~~~~~~~~~
5443
@@ -74,34 +63,6 @@ List difference ignoring order or duplicates
7463 >> > print (ddiff)
7564 {}
7665
77- Report repetitions
78- ~~~~~~~~~~~~~~~~~~
79-
80- This flag ONLY works when ignoring order is enabled.
81-
82- .. code :: python
83-
84- t1 = [1 , 3 , 1 , 4 ]
85- t2 = [4 , 4 , 1 ]
86- ddiff = DeepDiff(t1, t2, ignore_order = True , report_repetition = True )
87- print (ddiff)
88-
89- which will print you:
90-
91- .. code :: python
92-
93- {' iterable_item_removed' : {' root[1]' : 3 },
94- ' repetition_change' : {' root[0]' : {' old_repeat' : 2 ,
95- ' old_indexes' : [0 , 2 ],
96- ' new_indexes' : [2 ],
97- ' value' : 1 ,
98- ' new_repeat' : 1 },
99- ' root[3]' : {' old_repeat' : 1 ,
100- ' old_indexes' : [3 ],
101- ' new_indexes' : [0 , 1 ],
102- ' value' : 4 ,
103- ' new_repeat' : 2 }}}
104-
10566 Exclude types or paths
10667~~~~~~~~~~~~~~~~~~~~~~
10768
@@ -117,16 +78,6 @@ Exclude certain types from comparison
11778 >> > print (DeepDiff(t1, t2, exclude_types = {logging.Logger}))
11879 {}
11980
120- Exclude part of your object tree from comparison
121- ------------------------------------------------
122-
123- .. code :: python
124-
125- >> > t1 = {" for life" : " vegan" , " ingredients" : [" no meat" , " no eggs" , " no dairy" ]}
126- >> > t2 = {" for life" : " vegan" , " ingredients" : [" veggies" , " tofu" , " soy sauce" ]}
127- >> > print (DeepDiff(t1, t2, exclude_paths = {" root['ingredients']" }))
128- {}
129-
13081 Significant Digits
13182~~~~~~~~~~~~~~~~~~
13283
@@ -143,149 +94,23 @@ X=significant\_digits
14394 >> > DeepDiff(t1, t2, significant_digits = 1 )
14495 {' values_changed' : {' root' : {' old_value' : Decimal(' 1.52' ), ' new_value' : Decimal(' 1.57' )}}}
14596
146- Approximate float comparison:
147- -----------------------------
148-
149- .. code :: python
150-
151- >> > t1 = [ 1.1129 , 1.3359 ]
152- >> > t2 = [ 1.113 , 1.3362 ]
153- >> > pprint(DeepDiff(t1, t2, significant_digits = 3 ))
154- {}
155- >> > pprint(DeepDiff(t1, t2))
156- {' values_changed' : {' root[0]' : {' new_value' : 1.113 , ' old_value' : 1.1129 },
157- ' root[1]' : {' new_value' : 1.3362 , ' old_value' : 1.3359 }}}
158- >> > pprint(DeepDiff(1.23 * 10 ** 20 , 1.24 * 10 ** 20 , significant_digits = 1 ))
159- {' values_changed' : {' root' : {' new_value' : 1.24e+20 , ' old_value' : 1.23e+20 }}}
160-
161-
162- Views
163- ~~~~~
164-
165- Text View (default)
166- -------------------
167-
168- Text view is the original and currently the default view of DeepDiff.
169-
170- It is called text view because the results contain texts that represent the path to the data:
171-
172- Example of using the text view.
173- >>> from deepdiff import DeepDiff
174- >>> t1 = {1 :1 , 3 :3 , 4 :4 }
175- >>> t2 = {1 :1 , 3 :3 , 5 :5 , 6 :6 }
176- >>> ddiff = DeepDiff(t1, t2)
177- >>> print (ddiff)
178- {'dictionary_item_added': {'root[5]', 'root[6]'}, 'dictionary_item_removed': {'root[4]'}}
179-
180- So for example ddiff['dictionary_item_removed'] is a set if strings thus this is called the text view.
181-
182- .. seealso ::
183- The following examples are using the *default text view. *
184- The Tree View is introduced in DeepDiff v3 and provides traversing capabilities through your diffed data and more!
185- Read more about the Tree View at :doc: `/diff `
186-
187- Tree View (new)
188- ---------------
189-
190- Starting the version v3 You can choose the view into the deepdiff results.
191- The tree view provides you with tree objects that you can traverse through to find
192- the parents of the objects that are diffed and the actual objects that are being diffed.
193- This view is very useful when dealing with nested objects.
194- Note that tree view always returns results in the form of Python sets.
195-
196- You can traverse through the tree elements!
197-
198- .. note ::
199- The Tree view is just a different representation of the diffed data.
200- Behind the scene, DeepDiff creates the tree view first and then converts it to textual representation for the text view.
201-
202- .. code :: text
203-
204- +---------------------------------------------------------------+
205- | |
206- | parent(t1) parent node parent(t2) |
207- | + ^ + |
208- +------|--------------------------|---------------------|-------+
209- | | | up |
210- | Child | | | ChildRelationship
211- | Relationship | | |
212- | down | | |
213- +------|----------------------|-------------------------|-------+
214- | v v v |
215- | child(t1) child node child(t2) |
216- | |
217- +---------------------------------------------------------------+
218-
219-
220- The tree view allows you to have more than mere textual representaion of the diffed objects.
221- It gives you the actual objects (t1, t2) throughout the tree of parents and children.
222-
223- :Example:
224-
225- .. code :: python
226-
227- >> > t1 = {1 :1 , 2 :2 , 3 :3 }
228- >> > t2 = {1 :1 , 2 :4 , 3 :3 }
229- >> > ddiff_verbose0 = DeepDiff(t1, t2, verbose_level = 0 , view = ' tree' )
230- >> > ddiff_verbose0
231- {' values_changed' : {< root[2 ]> }}
232- >> >
233- >> > ddiff_verbose1 = DeepDiff(t1, t2, verbose_level = 1 , view = ' tree' )
234- >> > ddiff_verbose1
235- {' values_changed' : {< root[2 ] t1:2 , t2:4 > }}
236- >> > set_of_values_changed = ddiff_verbose1[' values_changed' ]
237- >> > # since set_of_values_changed includes only one item in a set
238- >> > # in order to get that one item we can:
239- >> > (changed,) = set_of_values_changed
240- >> > changed # Another way to get this is to do: changed=list(set_of_values_changed)[0]
241- < root[2 ] t1:2 , t2:4 >
242- >> > changed.t1
243- 2
244- >> > changed.t2
245- 4
246- >> > # You can traverse through the tree, get to the parents!
247- >> > changed.up
248- < root t1:{1 : 1 , 2 : 2 ,... }, t2:{1 : 1 , 2 : 4 ,... }>
249-
250- .. seealso ::
251- Read more about the Tree View at :doc: `/diff `
252-
253-
254- Verbose Level
255- ~~~~~~~~~~~~~
256-
257- Verbose level by default is 1. The possible values are 0, 1 and 2.
258-
259- - verbose_level 0: won’t report values when type changed.
260- - verbose_level 1: default
261- - verbose_level 2: will report values when custom objects or
262- dictionaries have items added or removed.
263-
264- .. seealso ::
265- Read more about the verbosity at :doc: `/diff `
266-
26797
26898 Serialization
26999~~~~~~~~~~~~~
270100
271- DeepDiff uses jsonpickle in order to serialize and deserialize its results into json. This works for both tree view and text view.
272-
273- :Serialize and then deserialize back to deepdiff:
101+ :Serialize to json:
274102
275103.. code :: python
276104
277105 >> > t1 = {1 : 1 , 2 : 2 , 3 : 3 }
278106 >> > t2 = {1 : 1 , 2 : " 2" , 3 : 3 }
279107 >> > ddiff = DeepDiff(t1, t2)
280- >> > jsoned = ddiff.json
108+ >> > jsoned = ddiff.to_json()
281109 >> > jsoned
282- ' {"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}}}'
283- >> > ddiff_new = DeepDiff.from_json(jsoned)
284- >> > ddiff == ddiff_new
285- True
110+ ' {"type_changes": {"root[2]": {"new_type": "str", "new_value": "2", "old_type": "int", "old_value": 2}}}'
286111
287112
288- Read more in
113+ And many more features! Read more in
289114
290115:doc: `/diff `
291116
@@ -373,7 +198,7 @@ But with DeepHash:
373198 >> > from deepdiff import DeepHash
374199 >> > obj = {1 : 2 , ' a' : ' b' }
375200 >> > DeepHash(obj)
376- {4355639248 : ( 2468916477072481777 , 512283587789292749 ), 4355639280 : ( - 3578777349255665377 , - 6377555218122431491 ), 4358636128 : ( - 8839064797231613815 , - 1822486391929534118 ), 4358009664 : ( 8833996863197925870 , - 419376694314494743 ), 4357467952 : ( 3415089864575009947 , 7987229399128149852 ) }
201+ {1 : 2468916477072481777512283587789292749 , 2 : - 35787773492556653776377555218122431491 , ... }
377202
378203 So what is exactly the hash of obj in this case?
379204DeepHash is calculating the hash of the obj and any other object that obj contains.
@@ -384,19 +209,12 @@ In order to get the hash of obj itself, you need to use the object (or the id of
384209
385210 >> > hashes = DeepHash(obj)
386211 >> > hashes[obj]
387- ( 3415089864575009947 , 7987229399128149852 )
212+ 34150898645750099477987229399128149852
388213
389214 Read more in the Deep Hash reference:
390215
391216:doc: `/deephash `
392217
393- .. _ignore\_ order : #ignore-order
394- .. _report\_ repetition : #report-repetitions
395- .. _verbose\_ level : #verbose-level
396- .. _exclude\_ types\_ or\_ paths : #exclude-types-or-paths
397- .. _significant\_ digits : #significant-digits
398- .. _views : #views
399-
400218
401219References
402220==========
0 commit comments