GeezerVison is showing github.com — updating docs · FOSSAware/deepdiff@cc0d0ab
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deepdiff/diff.py -9Lines changed: 0 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1253,15 +1253,6 @@ def _use_custom_operator(self, level): | 1253 | 1253 | Otherwise basically the 2 objects in the level are being omitted from the results. | 1254 | 1254 | """ | 1255 | 1255 |
| 1256 | | - # used = False | 1257 | | - | 1258 | | - # for operator in self.custom_operators: | 1259 | | - # if operator.match(level): | 1260 | | - # prevent_default = operator.diff(level, self) | 1261 | | - # used = True if prevent_default is None else prevent_default | 1262 | | - | 1263 | | - # return used | 1264 | | - | 1265 | 1256 | for operator in self.custom_operators: | 1266 | 1257 | if operator.match(level): | 1267 | 1258 | prevent_default = operator.give_up_diffing(level=level, diff_instance=self) | |
Collapse file Copy file name to clipboardExpand all lines: docs/custom.rst +69-2Lines changed: 69 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -133,6 +133,10 @@ if you are considering whether they are fruits or not. | 133 | 133 |
| 134 | 134 | In that case, you can pass a *custom_operators* for the job. | 135 | 135 |
| | 136 | +In fact, custom operators give you a lot of power. In the following examples we explore use cases from making DeepDiff | | 137 | +report the L2 Distance of items, to only include certain paths in diffing all the way to making DeepDiff stop diffing | | 138 | +as soon as the first diff is reported. | | 139 | + | 136 | 140 | To define an custom operator, you just need to inherit a *BaseOperator* and | 137 | 141 |
| 138 | 142 | * implement a give_up_diffing method | @@ -143,8 +147,31 @@ To define an custom operator, you just need to inherit a *BaseOperator* and | 143 | 147 | to report any diff. If you decide not to report anything, and this | 144 | 148 | function returns True, then the objects are basically skipped in the results. | 145 | 149 |
| 146 | | - * pass regex_paths and types that will be used to decide if the objects are matched. | 147 | | - one the objects are matched, then the give_up_diffing will be run to compare them. | | 150 | + * pass regex_paths and types that will be used to decide if the objects are matched to the init method. | | 151 | + once the objects are matched, then the give_up_diffing will be run to compare them. | | 152 | + | | 153 | +In fact you don't even have to subclass the base operator. | | 154 | + | | 155 | +This is all that is expected from the operator: | | 156 | + | | 157 | + def _use_custom_operator(self, level): | | 158 | + """ | | 159 | + For each level we check all custom operators. | | 160 | + If any one of them was a match for the level, we run the diff of the operator. | | 161 | + If the operator returned True, the operator must have decided these objects should not | | 162 | + be compared anymore. It might have already reported their results. | | 163 | + In that case the report will appear in the final results of this diff. | | 164 | + Otherwise basically the 2 objects in the level are being omitted from the results. | | 165 | + """ | | 166 | + | | 167 | + for operator in self.custom_operators: | | 168 | + if operator.match(level): | | 169 | + prevent_default = operator.give_up_diffing(level=level, diff_instance=self) | | 170 | + if prevent_default: | | 171 | + return True | | 172 | + | | 173 | + return False | | 174 | + | 148 | 175 |
| 149 | 176 |
| 150 | 177 | **Example 1: An operator that mapping L2:distance as diff criteria and reports the distance** | @@ -224,5 +251,45 @@ To define an custom operator, you just need to inherit a *BaseOperator* and | 224 | 251 | {'dictionary_item_added': [root.dict['a'], root.dict['b']], 'dictionary_item_removed': [root.dict['c'], root.dict['d']], 'values_changed': {"root.dict['list'][3]": {'new_value': 4, 'old_value': 2}}} | 225 | 252 | >>> | 226 | 253 |
| | 254 | +**Example 3: Only diff certain path's** | | 255 | + | | 256 | + >>> from deepdiff import DeepDiff | | 257 | + >>> class MyOperator: | | 258 | + ... def __init__(self, include_paths): | | 259 | + ... self.include_paths = include_paths | | 260 | + ... def match(self, level) -> bool: | | 261 | + ... return True | | 262 | + ... def give_up_diffing(self, level, diff_instance) -> bool: | | 263 | + ... return level.path() not in self.include_paths | | 264 | + ... | | 265 | + >>> | | 266 | + >>> t1 = {'a': [10, 11], 'b': [20, 21], 'c': [30, 31]} | | 267 | + >>> t2 = {'a': [10, 22], 'b': [20, 33], 'c': [30, 44]} | | 268 | + >>> | | 269 | + >>> DeepDiff(t1, t2, custom_operators=[ | | 270 | + ... MyOperator(include_paths="root['a'][1]") | | 271 | + ... ]) | | 272 | + {'values_changed': {"root['a'][1]": {'new_value': 22, 'old_value': 11}}} | | 273 | + | | 274 | +**Example 4: Give up further diffing once the first diff is found** | | 275 | + | | 276 | +Sometimes all you care about is that there is a difference between 2 objects and not all the details of what exactly is different. | | 277 | +In that case you may want to stop diffing as soon as the first diff is found. | | 278 | + | | 279 | + >>> from deepdiff import DeepDiff | | 280 | + >>> class MyOperator: | | 281 | + ... def match(self, level) -> bool: | | 282 | + ... return True | | 283 | + ... def give_up_diffing(self, level, diff_instance) -> bool: | | 284 | + ... return any(diff_instance.tree.values()) | | 285 | + ... | | 286 | + >>> t1 = [[1, 2], [3, 4], [5, 6]] | | 287 | + >>> t2 = [[1, 3], [3, 5], [5, 7]] | | 288 | + >>> | | 289 | + >>> DeepDiff(t1, t2, custom_operators=[ | | 290 | + ... MyOperator() | | 291 | + ... ]) | | 292 | + {'values_changed': {'root[0][1]': {'new_value': 3, 'old_value': 2}}} | | 293 | + | 227 | 294 |
| 228 | 295 | Back to :doc:`/index` |
Collapse file Copy file name to clipboardExpand all lines: tests/test_operators.py +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -174,3 +174,46 @@ def give_up_diffing(self, level, diff_instance): | 174 | 174 | 'values_changed': {"root.dict['list'][3]": {'new_value': 4, 'old_value': 2}}} | 175 | 175 |
| 176 | 176 | assert expected == ddiff2 | | 177 | + | | 178 | + def test_include_only_certain_path(self): | | 179 | + | | 180 | + class MyOperator: | | 181 | + | | 182 | + def __init__(self, include_paths): | | 183 | + self.include_paths = include_paths | | 184 | + | | 185 | + def match(self, level) -> bool: | | 186 | + return True | | 187 | + | | 188 | + def give_up_diffing(self, level, diff_instance) -> bool: | | 189 | + return level.path() not in self.include_paths | | 190 | + | | 191 | + t1 = {'a': [10, 11], 'b': [20, 21], 'c': [30, 31]} | | 192 | + t2 = {'a': [10, 22], 'b': [20, 33], 'c': [30, 44]} | | 193 | + | | 194 | + ddiff = DeepDiff(t1, t2, custom_operators=[ | | 195 | + MyOperator(include_paths="root['a'][1]") | | 196 | + ]) | | 197 | + | | 198 | + expected = {'values_changed': {"root['a'][1]": {'new_value': 22, 'old_value': 11}}} | | 199 | + assert expected == ddiff | | 200 | + | | 201 | + def test_give_up_diffing_on_first_diff(self): | | 202 | + | | 203 | + class MyOperator: | | 204 | + | | 205 | + def match(self, level) -> bool: | | 206 | + return True | | 207 | + | | 208 | + def give_up_diffing(self, level, diff_instance) -> bool: | | 209 | + return any(diff_instance.tree.values()) | | 210 | + | | 211 | + t1 = [[1, 2], [3, 4], [5, 6]] | | 212 | + t2 = [[1, 3], [3, 5], [5, 7]] | | 213 | + | | 214 | + ddiff = DeepDiff(t1, t2, custom_operators=[ | | 215 | + MyOperator() | | 216 | + ]) | | 217 | + | | 218 | + expected = {'values_changed': {'root[0][1]': {'new_value': 3, 'old_value': 2}}} | | 219 | + assert expected == ddiff |
|