GeezerVison is showing github.com — adding faq. fix for brackets in path. adding python 3.10 support. adding · FOSSAware/deepdiff@103f2d2
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: .github/workflows/main.yaml +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | runs-on: ubuntu-latest | 13 | 13 | strategy: | 14 | 14 | matrix: | 15 | | - python-version: [3.6, 3.7, 3.8, 3.9] | | 15 | + python-version: [3.6, 3.7, 3.8, 3.9, 3.10] | 16 | 16 | architecture: ["x64"] | 17 | 17 |
| 18 | 18 | steps: | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/diff.py +19-5Lines changed: 19 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1245,16 +1245,30 @@ def _auto_off_cache(self): | 1245 | 1245 |
| 1246 | 1246 | def _use_custom_operator(self, level): | 1247 | 1247 | """ | 1248 | | -
| | 1248 | + For each level we check all custom operators. | | 1249 | + If any one of them was a match for the level, we run the diff of the operator. | | 1250 | + If the operator returned True, the operator must have decided these objects should not | | 1251 | + be compared anymore. It might have already reported their results. | | 1252 | + In that case the report will appear in the final results of this diff. | | 1253 | + Otherwise basically the 2 objects in the level are being omitted from the results. | 1249 | 1254 | """ | 1250 | | - used = False | | 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 | 1251 | 1264 |
| 1252 | 1265 | for operator in self.custom_operators: | 1253 | 1266 | if operator.match(level): | 1254 | | - prevent_default = operator.diff(level, self) | 1255 | | - used = True if prevent_default is None else prevent_default | | 1267 | + prevent_default = operator.give_up_diffing(level=level, diff_instance=self) | | 1268 | + if prevent_default: | | 1269 | + return True | 1256 | 1270 |
| 1257 | | - return used | | 1271 | + return False | 1258 | 1272 |
| 1259 | 1273 | def _diff(self, level, parents_ids=frozenset(), _original_type=None): | 1260 | 1274 | """ | |
Collapse file Copy file name to clipboard +19-7Lines changed: 19 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import re | | 2 | +from deepdiff.helper import convert_item_or_items_into_compiled_regexes_else_none | 2 | 3 |
| 3 | 4 |
| 4 | 5 | class BaseOperator: | 5 | 6 | __operator_name__ = "__base__" | 6 | 7 |
| 7 | | - def __init__(self, path_regex): | 8 | | - self.path_regex = path_regex | 9 | | - self.regex = re.compile(f"^{self.path_regex}$") | | 8 | + def __init__(self, regex_paths=None, types=None): | | 9 | + if regex_paths: | | 10 | + self.regex_paths = convert_item_or_items_into_compiled_regexes_else_none(regex_paths) | | 11 | + else: | | 12 | + self.regex_paths = None | | 13 | + self.types = types | 10 | 14 |
| 11 | 15 | def match(self, level) -> bool: | 12 | | - matched = re.search(self.regex, level.path()) is not None | 13 | | - return matched | | 16 | + if self.regex_paths: | | 17 | + for pattern in self.regex_paths: | | 18 | + matched = re.search(pattern, level.path()) is not None | | 19 | + if matched: | | 20 | + return True | | 21 | + if self.types: | | 22 | + for type_ in self.types: | | 23 | + if isinstance(level.t1, type_) and isinstance(level.t2, type_): | | 24 | + return True | | 25 | + return False | 14 | 26 |
| 15 | | - def diff(self, level, instance) -> bool: | 16 | | - raise NotImplementedError | | 27 | + def give_up_diffing(self, level, diff_instance) -> bool: | | 28 | + raise NotImplementedError('Please implement the diff function.') |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/path.py +29-5Lines changed: 29 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,8 @@ class RootCanNotBeModified(ValueError): | 19 | 19 |
| 20 | 20 | def _add_to_elements(elements, elem, inside): | 21 | 21 | # Ignore private items | | 22 | + if not elem: | | 23 | + return | 22 | 24 | if not elem.startswith('__'): | 23 | 25 | try: | 24 | 26 | elem = literal_eval(elem) | @@ -49,14 +51,31 @@ def _path_to_elements(path, root_element=DEFAULT_FIRST_ELEMENT): | 49 | 51 | inside = False | 50 | 52 | prev_char = None | 51 | 53 | path = path[4:] # removing "root from the beginning" | | 54 | + brackets = [] | | 55 | + inside_quotes = False | 52 | 56 | for char in path: | 53 | 57 | if prev_char == '\\': | 54 | 58 | elem += char | | 59 | + elif char in {'"', "'"}: | | 60 | + elem += char | | 61 | + inside_quotes = not inside_quotes | | 62 | + if not inside_quotes: | | 63 | + _add_to_elements(elements, elem, inside) | | 64 | + elem = '' | | 65 | + elif inside_quotes: | | 66 | + elem += char | 55 | 67 | elif char == '[': | 56 | 68 | if inside == '.': | 57 | 69 | _add_to_elements(elements, elem, inside) | 58 | | - inside = '[' | 59 | | - elem = '' | | 70 | + inside = '[' | | 71 | + elem = '' | | 72 | + # we are already inside. The bracket is a part of the word. | | 73 | + elif inside == '[': | | 74 | + elem += char | | 75 | + else: | | 76 | + inside = '[' | | 77 | + brackets.append('[') | | 78 | + elem = '' | 60 | 79 | elif char == '.': | 61 | 80 | if inside == '[': | 62 | 81 | elem += char | @@ -67,9 +86,14 @@ def _path_to_elements(path, root_element=DEFAULT_FIRST_ELEMENT): | 67 | 86 | inside = '.' | 68 | 87 | elem = '' | 69 | 88 | elif char == ']': | 70 | | - _add_to_elements(elements, elem, inside) | 71 | | - elem = '' | 72 | | - inside = False | | 89 | + if brackets and brackets[-1] == '[': | | 90 | + brackets.pop() | | 91 | + if brackets: | | 92 | + elem += char | | 93 | + else: | | 94 | + _add_to_elements(elements, elem, inside) | | 95 | + elem = '' | | 96 | + inside = False | 73 | 97 | else: | 74 | 98 | elem += char | 75 | 99 | prev_char = char | |
Collapse file Copy file name to clipboardExpand all lines: docs/Makefile +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,7 @@ I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . | 23 | 23 |
| 24 | 24 | help: | 25 | 25 | @echo "Please use \`make <target>' where <target> is one of" | | 26 | + @echo " buildme echos what to run to do live builds." | 26 | 27 | @echo " html to make standalone HTML files" | 27 | 28 | @echo " dirhtml to make HTML files named index.html in directories" | 28 | 29 | @echo " singlehtml to make a single large HTML file" | | 48 | 49 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" | 49 | 50 | @echo " coverage to run coverage check of the documentation (if enabled)" | 50 | 51 |
| | 52 | +buildme: | | 53 | + @echo "Please make sure the .env is pointing to the right path for the build. Then run ./buildme.py" | | 54 | + | 51 | 55 | clean: | 52 | 56 | rm -rf $(BUILDDIR)/* | 53 | 57 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/custom.rst +76-36Lines changed: 76 additions & 36 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | :doc:`/index` | 2 | 2 |
| 3 | | -Other Parameters | 4 | | -================ | | 3 | +Customized Diff | | 4 | +=============== | 5 | 5 |
| 6 | 6 | .. _iterable_compare_func_label: | 7 | 7 |
| @@ -122,67 +122,107 @@ For example you could use the level object to further determine if the 2 objects | 122 | 122 |
| 123 | 123 | The level parameter of the iterable_compare_func is only used when ignore_order=False which is the default value for ignore_order. | 124 | 124 |
| | 125 | + | 125 | 126 | .. _custom_operators_label: | 126 | 127 |
| 127 | 128 | Custom Operators | 128 | | ---------------------- | | 129 | +---------------- | 129 | 130 |
| 130 | 131 | Whether two objects are different or not are largely depend on the context. For example, apple and banana are the same | 131 | | -if you are considering whether the | | 132 | +if you are considering whether they are fruits or not. | 132 | 133 |
| 133 | | -*custom_operators* is for the job. | | 134 | +In that case, you can pass a *custom_operators* for the job. | 134 | 135 |
| 135 | 136 | To define an custom operator, you just need to inherit a *BaseOperator* and | 136 | | - * implement method | 137 | | - * diff(level: DiffLevel, instance: DeepDiff) -> boolean | 138 | | - * to do custom diff logic with full access to DeepDiff instance | 139 | | - * you can use instance.custom_report_result to record info | 140 | | - * to return a boolean value to determine whether the process | 141 | | - should quit or continue with default behavior | 142 | 137 |
| | 138 | + * implement a give_up_diffing method | | 139 | + * give_up_diffing(level: DiffLevel, diff_instance: DeepDiff) -> boolean | | 140 | + | | 141 | + If it returns True, then we will give up diffing the 2 objects. | | 142 | + You may or may not use the diff_instance.custom_report_result within this function | | 143 | + to report any diff. If you decide not to report anything, and this | | 144 | + function returns True, then the objects are basically skipped in the results. | | 145 | + | | 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. | 143 | 148 |
| 144 | | -An operator that mapping L2:distance as diff criteria | | 149 | + | | 150 | +**Example 1: An operator that mapping L2:distance as diff criteria and reports the distance** | | 151 | + | | 152 | + >>> import math | | 153 | + >>> | | 154 | + >>> from typing import List | 145 | 155 | >>> from deepdiff import DeepDiff | 146 | 156 | >>> from deepdiff.operator import BaseOperator | 147 | 157 | >>> | | 158 | + >>> | | 159 | + >>> class L2DistanceDifferWithPreventDefault(BaseOperator): | | 160 | + ... def __init__(self, regex_paths: List[str], distance_threshold: float): | | 161 | + ... super().__init__(regex_paths) | | 162 | + ... self.distance_threshold = distance_threshold | | 163 | + ... def _l2_distance(self, c1, c2): | | 164 | + ... return math.sqrt( | | 165 | + ... (c1["x"] - c2["x"]) ** 2 + (c1["y"] - c2["y"]) ** 2 | | 166 | + ... ) | | 167 | + ... def give_up_diffing(self, level, diff_instance): | | 168 | + ... l2_distance = self._l2_distance(level.t1, level.t2) | | 169 | + ... if l2_distance > self.distance_threshold: | | 170 | + ... diff_instance.custom_report_result('distance_too_far', level, { | | 171 | + ... "l2_distance": l2_distance | | 172 | + ... }) | | 173 | + ... return True | | 174 | + ... | | 175 | + >>> | 148 | 176 | >>> t1 = { | 149 | 177 | ... "coordinates": [ | 150 | 178 | ... {"x": 5, "y": 5}, | 151 | 179 | ... {"x": 8, "y": 8} | 152 | 180 | ... ] | 153 | 181 | ... } | 154 | | - ... | | 182 | + >>> | 155 | 183 | >>> t2 = { | 156 | 184 | ... "coordinates": [ | 157 | 185 | ... {"x": 6, "y": 6}, | 158 | 186 | ... {"x": 88, "y": 88} | 159 | 187 | ... ] | 160 | 188 | ... } | | 189 | + >>> DeepDiff(t1, t2, custom_operators=[L2DistanceDifferWithPreventDefault( | | 190 | + ... ["^root\\['coordinates'\\]\\[\\d+\\]$"], | | 191 | + ... 1 | | 192 | + ... )]) | | 193 | + {'distance_too_far': {"root['coordinates'][0]": {'l2_distance': 1.4142135623730951}, "root['coordinates'][1]": {'l2_distance': 113.13708498984761}}} | | 194 | + | | 195 | + | | 196 | +**Example 2: If the objects are subclasses of a certain type, only compare them if their list attributes are not equal sets** | | 197 | + | | 198 | + >>> class CustomClass: | | 199 | + ... def __init__(self, d: dict, l: list): | | 200 | + ... self.dict = d | | 201 | + ... self.dict['list'] = l | 161 | 202 | ... | 162 | | - >>> class L2DistanceDifferWithPreventDefault(BaseOperator): | 163 | | - ... def __init__(self, distance_threshold): | 164 | | - ... self.distance_threshold = distance_threshold | 165 | | - ... | 166 | | - ... def _l2_distance(self, c1, c2): | 167 | | - ... return math.sqrt( | 168 | | - ... (c1["x"] - c2["x"]) ** 2 + (c1["y"] - c2["y"]) ** 2 | 169 | | - ... ) | 170 | | - ... # you can also override match method | 171 | | - ... # def match(self, level): | 172 | | - ... # return True | 173 | | - ... | 174 | | - ... def diff(self, level, diff_instance): | 175 | | - ... l2_distance = self._l2_distance(level.t1, level.t2) | 176 | | - ... if l2_distance > self.distance_threshold: | 177 | | - ... diff_instance.custom_report_result('distance_too_far', level, { | 178 | | - ... "l2_distance": l2_distance | 179 | | - ... }) | 180 | | - ... # | 181 | | - ... return True | | 203 | + >>> | | 204 | + >>> custom1 = CustomClass(d=dict(a=1, b=2), l=[1, 2, 3]) | | 205 | + >>> custom2 = CustomClass(d=dict(c=3, d=4), l=[1, 2, 3, 2]) | | 206 | + >>> custom3 = CustomClass(d=dict(a=1, b=2), l=[1, 2, 3, 4]) | | 207 | + >>> | | 208 | + >>> | | 209 | + >>> class ListMatchOperator(BaseOperator): | | 210 | + ... def give_up_diffing(self, level, diff_instance): | | 211 | + ... if set(level.t1.dict['list']) == set(level.t2.dict['list']): | | 212 | + ... return True | 182 | 213 | ... | 183 | | - >>> DeepDiff(t1, t2, custom_operators=[L2DistanceDifferWithPreventDefault(1)]) | 184 | | - {'distance_too_far': {"root['coordinates'][0]": {'l2_distance': 1.4142135623730951}, | 185 | | - "root['coordinates'][1]": {'l2_distance': 113.13708498984761}}} | | 214 | + >>> | | 215 | + >>> DeepDiff(custom1, custom2, custom_operators=[ | | 216 | + ... ListMatchOperator(types=[CustomClass]) | | 217 | + ... ]) | | 218 | + {} | | 219 | + >>> | | 220 | + >>> | | 221 | + >>> DeepDiff(custom2, custom3, custom_operators=[ | | 222 | + ... ListMatchOperator(types=[CustomClass]) | | 223 | + ... ]) | | 224 | + {'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 | + >>> | 186 | 226 |
| 187 | 227 |
| 188 | 228 | Back to :doc:`/index` |
Collapse file Copy file name to clipboardExpand all lines: docs/diff.rst +2-1Lines changed: 2 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,8 @@ DeepDiff | 23 | 23 | serialization | 24 | 24 | optimizations | 25 | 25 | stats | 26 | | - other | | 26 | + custom | | 27 | + faq | 27 | 28 | troubleshoot | 28 | 29 |
| 29 | 30 | Back to :doc:`/index` |
Collapse file Copy file name to clipboardExpand all lines: docs/diff_doc.rst +3-1Lines changed: 3 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | DeepDiff | 4 | 4 | ======== | 5 | 5 |
| 6 | | - | 7 | 6 | Deep Difference of dictionaries, iterables, strings and almost any other object. | 8 | 7 | It will recursively look for all the changes. | 9 | 8 |
| @@ -34,6 +33,9 @@ cache_purge_level: int, 0, 1, or 2. default=1 | 34 | 33 | cache_tuning_sample_size : int >= 0, default = 0 | 35 | 34 | :ref:`cache_tuning_sample_size_label` This is an experimental feature. It works hands in hands with the :ref:`cache_size_label`. When cache_tuning_sample_size is set to anything above zero, it will sample the cache usage with the passed sample size and decide whether to use the cache or not. And will turn it back on occasionally during the diffing process. This option can be useful if you are not sure if you need any cache or not. However you will gain much better performance with keeping this parameter zero and running your diff with different cache sizes and benchmarking to find the optimal cache size. | 36 | 35 |
| | 36 | +custom_operators : BaseOperator subclasses, default = None | | 37 | + :ref:`custom_operators_label` if you are considering whether they are fruits or not. In that case, you can pass a *custom_operators* for the job. | | 38 | + | 37 | 39 | exclude_paths: list, default = None | 38 | 40 | :ref:`exclude_paths_label` | 39 | 41 | List of paths to exclude from the report. If only one item, you can path it as a string. | |
|