@@ -180,14 +180,16 @@ Define A Custom Operator |
180 | 180 |
|
181 | 181 | To define an custom operator, you just need to inherit *BaseOperator* or *BaseOperatorPlus*. |
182 | 182 |
|
183 | | -*BaseOperatorPlus* is our new base operator that can be subclassed and provides the structure to build any custom operator. |
184 | | -*BaseOperator* is our older base operator that was designed mainly for simple string based regex comparison. |
| 183 | + - *BaseOperatorPlus* is our new base operator that can be subclassed and provides the structure to build any custom operator. |
| 184 | + - *BaseOperator* is our older base class for creating custom operators. It was designed mainly for simple string based regex comparison. |
| 185 | + |
185 | 186 |
|
186 | 187 | Base Operator Plus |
187 | | ------------------- |
| 188 | +.................. |
188 | 189 |
|
189 | 190 | *BaseOperatorPlus* is our new base operator that can be subclassed and provides the structure to build any custom operator. |
190 | 191 |
|
| 192 | +.. code-block:: python |
191 | 193 |
|
192 | 194 | class BaseOperatorPlus(metaclass=ABCMeta): |
193 | 195 |
|
@@ -216,74 +218,8 @@ Base Operator Plus |
216 | 218 | pass |
217 | 219 |
|
218 | 220 |
|
219 | | -**Example 1: We don't care about the exact GUID values. As long as pairs of strings match GUID regex, we want them to be considered as equals |
220 | | - >>> import re |
221 | | - ... from typing import Any |
222 | | - ... from deepdiff import DeepDiff |
223 | | - ... from deepdiff.operator import BaseOperatorPlus |
224 | | - ... |
225 | | - ... |
226 | | - ... |
227 | | - ... d1 = { |
228 | | - ... "Name": "SUB_OBJECT_FILES", |
229 | | - ... "Values": { |
230 | | - ... "Value": [ |
231 | | - ... "{f254498b-b752-4f35-bef5-6f1844b61eb7}", |
232 | | - ... "{7fb2a550-1849-45c0-b273-9aa5e4eb9f2b}", |
233 | | - ... "{a9cbecc0-21dc-49ce-8b2c-d36352dae139}" |
234 | | - ... ] |
235 | | - ... } |
236 | | - ... } |
237 | | - ... |
238 | | - ... d2 = { |
239 | | - ... "Name": "SUB_OBJECT_FILES", |
240 | | - ... "Values": { |
241 | | - ... "Value": [ |
242 | | - ... "{e5d18917-1a2c-4abe-b601-8ec002629953}", |
243 | | - ... "{ea71ba1f-1339-4fae-bc28-a9ce9b8a8c67}", |
244 | | - ... "{66bb6192-9cd2-4074-8be1-f2ac52877c70}", |
245 | | - ... ] |
246 | | - ... } |
247 | | - ... } |
248 | | - ... |
249 | | - ... |
250 | | - ... |
251 | | - ... class RemoveGUIDsOperator(BaseOperatorPlus): |
252 | | - ... _pattern = r"[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}" |
253 | | - ... _substitute = "guid" |
254 | | - ... |
255 | | - ... def match(self, level) -> bool: |
256 | | - ... return isinstance(level.t1, str) and isinstance(level.t2, str) |
257 | | - ... |
258 | | - ... @classmethod |
259 | | - ... def _remove_pattern(cls, t: str): |
260 | | - ... return re.sub(cls._pattern, cls._substitute, t) |
261 | | - ... |
262 | | - ... def give_up_diffing(self, level, diff_instance): |
263 | | - ... t1 = self._remove_pattern(level.t1) |
264 | | - ... t2 = self._remove_pattern(level.t2) |
265 | | - ... return t1 == t2 |
266 | | - ... |
267 | | - ... def normalize_value_for_hashing(self, parent: Any, obj: Any) -> Any: |
268 | | - ... """ |
269 | | - ... Used for ignore_order=True |
270 | | - ... """ |
271 | | - ... if isinstance(obj, str): |
272 | | - ... return self._remove_pattern(obj) |
273 | | - ... return obj |
274 | | - ... |
275 | | - ... |
276 | | - ... operator = RemoveGUIDsOperator() |
277 | | - ... |
278 | | - ... diff1 = DeepDiff(d1, d2, custom_operators=[operator], log_stacktrace=True) |
279 | | - ... diff1 |
280 | | - ... |
281 | | - ... |
282 | | - ... diff2 = DeepDiff(d1, d2, ignore_order=True, custom_operators=[operator], log_stacktrace=True) |
283 | | - ... diff2 |
284 | | - ... |
285 | | - ... |
286 | | - {} |
| 221 | +**Example 1: We don't care about the exact GUID values. As long as pairs of strings match GUID regex, we want them to be considered as equals** |
| 222 | + |
287 | 223 | >>> import re |
288 | 224 | ... from typing import Any |
289 | 225 | ... from deepdiff import DeepDiff |
@@ -340,24 +276,22 @@ Base Operator Plus |
340 | 276 | ... |
341 | 277 | ... operator = RemoveGUIDsOperator() |
342 | 278 | ... |
343 | | - ... diff1 = DeepDiff(d1, d2, custom_operators=[operator], log_stacktrace=True) |
| 279 | + >>> diff1 = DeepDiff(d1, d2, custom_operators=[operator], log_stacktrace=True) |
344 | 280 | ... diff1 |
345 | | - ... |
346 | 281 | {} |
347 | 282 | >>> diff2 = DeepDiff(d1, d2, ignore_order=True, custom_operators=[operator], log_stacktrace=True) |
348 | 283 | ... diff2 |
349 | | - ... |
350 | | - ... |
351 | 284 | {} |
352 | 285 |
|
353 | 286 |
|
354 | 287 |
|
355 | | - |
356 | 288 | Base Operator |
357 | | -------------- |
| 289 | +............. |
| 290 | + |
| 291 | +*BaseOperator* is our older base class for creating custom operators. It was designed mainly for simple string based regex comparison. |
358 | 292 |
|
359 | | -*BaseOperator* is our older base operator that was designed mainly for simple string based regex comparison. |
360 | 293 |
|
| 294 | +.. code-block:: python |
361 | 295 |
|
362 | 296 | class BaseOperator: |
363 | 297 |
|
|