@@ -12,12 +12,13 @@ Tested on Python 2.7, 3.3, 3.4, 3.5, Pypy, Pypy3
12
12
## Table of Contents
13
13
14
14
- [ Installation] ( #Installation )
15
- - [ Importing] ( #Importing )
16
- - [ Supported data types] ( #supported-data-types )
17
- - [ Type Change] ( #type-of-an-item-has-changed )
18
- - [ Value Change] ( #value-of-an-item-has-changed )
19
- - [ Item added or removed] ( #item-added-or-removed )
20
- - [ String difference] ( #string-difference )
15
+ - [ Parameters] ( #parameters )
16
+ - [ Ignore Order] ( #ignore-order )
17
+ - [ Report repetitions] ( #report-repetitions )
18
+ - [ Verbose Level] ( #verbose-level )
19
+ - [ Examples] ( #examples )
20
+ - [ Documentation] ( http://deepdiff.readthedocs.io/en/latest/ )
21
+
21
22
22
23
##Installation
23
24
@@ -31,10 +32,67 @@ Tested on Python 2.7, 3.3, 3.4, 3.5, Pypy, Pypy3
31
32
>> > from deepdiff import DeepDiff
32
33
```
33
34
35
+ ## Parameters
36
+
37
+ In addition to the 2 objects being compared:
38
+
39
+ - [ ignore_order] ( #ignore-order )
40
+ - [ report_repetition] ( #report-repetitions )
41
+ - [ verbose_level] ( #verbose-level )
42
+
34
43
## Supported data types
35
44
36
45
int, string, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple and custom objects!
37
46
47
+ ## Ignore Order
48
+
49
+ Sometimes you don't care about the order of objects when comparing them. In those cases, you can set ` ignore_order=True ` . However this flag won't report the repetitions to you. You need to additionally enable ` report_report_repetition=True ` for getting a report of repetitions.
50
+
51
+ ### List difference ignoring order or duplicates
52
+
53
+ ``` python
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" :[1 , 3 , 2 , 3 ]}}
56
+ >> > ddiff = DeepDiff(t1, t2, ignore_order = True )
57
+ >> > print (ddiff)
58
+ {}
59
+ ```
60
+
61
+ ## Report repetitions
62
+
63
+ This flag ONLY works when ignoring order is enabled.
64
+
65
+ ``` python
66
+ t1 = [1 , 3 , 1 , 4 ]
67
+ t2 = [4 , 4 , 1 ]
68
+ ddiff = DeepDiff(t1, t2, ignore_order = True , report_repetition = True )
69
+ print (ddiff)
70
+ ```
71
+
72
+ which will print you:
73
+
74
+ ``` python
75
+ {' iterable_item_removed' : {' root[1]' : 3 },
76
+ ' repetition_change' : {' root[0]' : {' old_repeat' : 2 ,
77
+ ' old_indexes' : [0 , 2 ],
78
+ ' new_indexes' : [2 ],
79
+ ' value' : 1 ,
80
+ ' new_repeat' : 1 },
81
+ ' root[3]' : {' old_repeat' : 1 ,
82
+ ' old_indexes' : [3 ],
83
+ ' new_indexes' : [0 , 1 ],
84
+ ' value' : 4 ,
85
+ ' new_repeat' : 2 }}}
86
+ ```
87
+
88
+ ## Verbose Level
89
+
90
+ Verbose level by default is 1. The possible values are 0, 1 and 2.
91
+
92
+ - Verbose level 0: won't report values when type changed. [ Example] ( ##type-of-an-item-has-changed )
93
+ - Verbose level 1: default
94
+ - Verbose level 2: will report values when custom objects or dictionaries have items added or removed. [ Example] ( #items-added-or-removed-verbose )
95
+
38
96
39
97
## Examples
40
98
@@ -67,6 +125,17 @@ int, string, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple an
67
125
' old_value' : 2 }}}
68
126
```
69
127
128
+ And if you don't care about the value of items that have changed type, please set verbose level to 0:
129
+
130
+ ``` python
131
+ >> > t1 = {1 :1 , 2 :2 , 3 :3 }
132
+ >> > t2 = {1 :1 , 2 :" 2" , 3 :3 }
133
+ >> > pprint(DeepDiff(t1, t2), indent = 2 )
134
+ { ' type_changes' : { ' root[2]' : { ' new_type' : < class ' str' > ,
135
+ ' old_type' : < class ' int' > ,}}}
136
+ ```
137
+
138
+
70
139
### Value of an item has changed
71
140
72
141
``` python
@@ -79,13 +148,25 @@ int, string, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple an
79
148
### Item added or removed
80
149
81
150
``` python
82
- >> > t1 = {1 :1 , 2 : 2 , 3 :3 , 4 :4 }
83
- >> > t2 = {1 :1 , 2 : 4 , 3 :3 , 5 :5 , 6 :6 }
151
+ >> > t1 = {1 :1 , 3 :3 , 4 :4 }
152
+ >> > t2 = {1 :1 , 3 :3 , 5 :5 , 6 :6 }
84
153
>> > ddiff = DeepDiff(t1, t2)
85
- >> > pprint (ddiff)
86
- {' dictionary_item_added' : [' root[5]' , ' root[6]' ],
87
- ' dictionary_item_removed' : [' root[4]' ],
88
- ' values_changed' : {' root[2]' : {' new_value' : 4 , ' old_value' : 2 }}}
154
+ >> > pprint(ddiff)
155
+ {' dictionary_item_added' : {' root[5]' , ' root[6]' },
156
+ ' dictionary_item_removed' : {' root[4]' }}
157
+ ```
158
+
159
+ #### Items added or removed verbose
160
+
161
+ And if you would like to know the values of items added or removed, please set the verbose_level to 2:
162
+
163
+ ``` python
164
+ >> > t1 = {1 :1 , 3 :3 , 4 :4 }
165
+ >> > t2 = {1 :1 , 3 :3 , 5 :5 , 6 :6 }
166
+ >> > ddiff = DeepDiff(t1, t2, verbose_level = 2 )
167
+ >> > pprint(ddiff, indent = 2 )
168
+ { ' dictionary_item_added' : {' root[5]' : 5 , ' root[6]' : 6 },
169
+ ' dictionary_item_removed' : {' root[4]' : 4 }}
89
170
```
90
171
91
172
### String difference
@@ -136,19 +217,6 @@ int, string, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple an
136
217
End
137
218
```
138
219
139
- ### Type change
140
-
141
- ``` python
142
- >> > t1 = {1 :1 , 2 :2 , 3 :3 , 4 :{" a" :" hello" , " b" :[1 , 2 , 3 ]}}
143
- >> > t2 = {1 :1 , 2 :2 , 3 :3 , 4 :{" a" :" hello" , " b" :" world\n\n\n End" }}
144
- >> > ddiff = DeepDiff(t1, t2)
145
- >> > pprint (ddiff, indent = 2 )
146
- { ' type_changes' : { " root[4]['b']" : { ' new_type' : < class ' str' > ,
147
- ' new_value' : ' world\n\n\n End' ,
148
- ' old_type' : < class ' list' > ,
149
- ' old_value' : [1 , 2 , 3 ]}}}
150
- ```
151
-
152
220
### List difference
153
221
154
222
``` python
@@ -159,7 +227,7 @@ int, string, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple an
159
227
{' iterable_item_removed' : {" root[4]['b'][2]" : 3 , " root[4]['b'][3]" : 4 }}
160
228
```
161
229
162
- ### List difference 2:
230
+ ### List difference Example 2
163
231
164
232
``` python
165
233
>> > t1 = {1 :1 , 2 :2 , 3 :3 , 4 :{" a" :" hello" , " b" :[1 , 2 , 3 ]}}
@@ -171,38 +239,6 @@ int, string, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple an
171
239
" root[4]['b'][2]" : {' new_value' : 2 , ' old_value' : 3 }}}
172
240
```
173
241
174
- ### List difference ignoring order or duplicates: (with the same dictionaries as above)
175
-
176
- ``` python
177
- >> > t1 = {1 :1 , 2 :2 , 3 :3 , 4 :{" a" :" hello" , " b" :[1 , 2 , 3 ]}}
178
- >> > t2 = {1 :1 , 2 :2 , 3 :3 , 4 :{" a" :" hello" , " b" :[1 , 3 , 2 , 3 ]}}
179
- >> > ddiff = DeepDiff(t1, t2, ignore_order = True )
180
- >> > print (ddiff)
181
- {}
182
- ```
183
-
184
- ### List difference ignoring order or duplicates for objects that have hash collision (Advanced)
185
-
186
- It might rarely happen that Python objects have hash collision using the built-in hash function:
187
-
188
- ``` python
189
- >> > a= ' \0 B'
190
- >> > hash (a)
191
- 64
192
- >> > b= ' \0\0 C'
193
- >> > hash (b)
194
- 64
195
- ```
196
-
197
- Although ` ignore_order ` flag uses hash of items to calculate what is added or removed, we still get the right answer.
198
-
199
- ``` python
200
- >> > list1= [a,b]
201
- >> > list2= [a,a]
202
- >> > DeepDiff(list1,list2, ignore_order = True )
203
- {' set_item_removed' : set ([" root['\x00\x00 C']" ])}
204
- ```
205
-
206
242
### List that contains dictionary:
207
243
208
244
``` python
@@ -341,7 +377,7 @@ And here is more info: <http://zepworks.com/blog/diff-it-to-digg-it/>
341
377
342
378
##Documentation
343
379
344
- < http://deepdiff.readthedocs.org /en/latest/ >
380
+ < http://deepdiff.readthedocs.io /en/latest/ >
345
381
346
382
##Changelog
347
383
0 commit comments