GeezerVison is showing github.com — wip · adjust/deepdiff@72393b8
This repository was archived by the owner on Dec 11, 2023. It is now read-only.
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/commands.py +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | CUTOFF_INTERSECTION_FOR_PAIRS_DEFAULT, | 9 | 9 | logger | 10 | 10 | ) | 11 | | -from deepdiff import Delta, DeepSearch, extract | | 11 | +from deepdiff import Delta, DeepSearch, extract as deep_extract | 12 | 12 | from deepdiff.serialization import load_path_content, save_content_to_path | 13 | 13 |
| 14 | 14 |
| @@ -30,6 +30,7 @@ def cli(): | 30 | 30 | @click.option('--exclude-paths', required=False, type=str, show_default=False, multiple=True) | 31 | 31 | @click.option('--exclude-regex-paths', required=False, type=str, show_default=False, multiple=True) | 32 | 32 | @click.option('--get-deep-distance', is_flag=True, show_default=True) | | 33 | +@click.option('--group-by', required=False, type=str, show_default=False, multiple=False) | 33 | 34 | @click.option('--ignore-order', is_flag=True, show_default=True) | 34 | 35 | @click.option('--ignore-string-type-changes', is_flag=True, show_default=True) | 35 | 36 | @click.option('--ignore-numeric-type-changes', is_flag=True, show_default=True) | @@ -99,7 +100,7 @@ def diff( | 99 | 100 | @click.argument('path', type=click.Path(exists=True, resolve_path=True)) | 100 | 101 | @click.argument('delta_path', type=click.Path(exists=True, resolve_path=True)) | 101 | 102 | @click.option('--backup', '-b', is_flag=True, show_default=True) | 102 | | -def deeppatch( | | 103 | +def patch( | 103 | 104 | path, delta_path, backup | 104 | 105 | ): | 105 | 106 | try: | @@ -150,7 +151,7 @@ def deeppatch( | 150 | 151 | @click.option('--exclude-paths', required=False, type=str, show_default=False, multiple=True) | 151 | 152 | @click.option('--exclude-regex-paths', required=False, type=str, show_default=False, multiple=True) | 152 | 153 | @click.option('--verbose-level', required=False, default=1, type=click.IntRange(0, 2), show_default=True) | 153 | | -def deepgrep(item, path, **kwargs): | | 154 | +def grep(item, path, **kwargs): | 154 | 155 | kwargs['case_sensitive'] = not kwargs.pop('ignore_case') | 155 | 156 | kwargs['match_string'] = kwargs.pop('exact_match') | 156 | 157 |
| @@ -169,7 +170,7 @@ def deepgrep(item, path, **kwargs): | 169 | 170 | @cli.command() | 170 | 171 | @click.argument('path_inside', required=True, type=str) | 171 | 172 | @click.argument('path', type=click.Path(exists=True, resolve_path=True)) | 172 | | -def deepextract(path_inside, path): | | 173 | +def extract(path_inside, path): | 173 | 174 | """ | 174 | 175 | Deep Extract | 175 | 176 | """ | @@ -179,7 +180,7 @@ def deepextract(path_inside, path): | 179 | 180 | sys.exit(str(f"Error when loading {path}: {e}")) | 180 | 181 |
| 181 | 182 | try: | 182 | | - result = extract(content, path_inside) | | 183 | + result = deep_extract(content, path_inside) | 183 | 184 | except Exception as e: | 184 | 185 | sys.exit(str(f"Error when running deep search on {path}: {e}")) | 185 | 186 | pprint(result, indent=2) |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/diff.py +14-3Lines changed: 14 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,6 +114,7 @@ def __init__(self, | 114 | 114 | exclude_types=None, | 115 | 115 | exclude_obj_callback=None, | 116 | 116 | get_deep_distance=False, | | 117 | + group_by=None, | 117 | 118 | hasher=None, | 118 | 119 | hashes=None, | 119 | 120 | ignore_order=False, | @@ -149,7 +150,7 @@ def __init__(self, | 149 | 150 | "ignore_private_variables, ignore_nan_inequality, number_to_string_func, verbose_level, " | 150 | 151 | "view, hasher, hashes, max_passes, max_diffs, " | 151 | 152 | "cutoff_distance_for_pairs, cutoff_intersection_for_pairs, log_frequency_in_sec, cache_size, " | 152 | | - "cache_tuning_sample_size, get_deep_distance, cache_purge_level, " | | 153 | + "cache_tuning_sample_size, get_deep_distance, group_by, cache_purge_level, " | 153 | 154 | "_original_type, _parameters and _shared_parameters.") % ', '.join(kwargs.keys())) | 154 | 155 |
| 155 | 156 | if _parameters: | @@ -248,8 +249,18 @@ def __init__(self, | 248 | 249 | self._parameters = _parameters | 249 | 250 | self.deephash_parameters = self.__get_deephash_params() | 250 | 251 | self.tree = TreeResult() | 251 | | - self.t1 = t1 | 252 | | - self.t2 = t2 | | 252 | + if group_by: | | 253 | + try: | | 254 | + t1 = self.t1 = {row[group_by]: row for row in t1} | | 255 | + except KeyError: | | 256 | + logger.error("can not group t1 by {}".format(group_by)) | | 257 | + try: | | 258 | + t2 = self.t2 = {row[group_by]: row for row in t2} | | 259 | + except KeyError: | | 260 | + logger.error("can not group t2 by {}".format(group_by)) | | 261 | + else: | | 262 | + self.t1 = t1 | | 263 | + self.t2 = t2 | 253 | 264 |
| 254 | 265 | try: | 255 | 266 | root = DiffLevel(t1, t2, verbose_level=self.verbose_level) | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/serialization.py +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -346,6 +346,19 @@ def load_path_content(path, file_type=None): | 346 | 346 | if clevercsv is None: # pragma: no cover. | 347 | 347 | raise ImportError('CleverCSV needs to be installed.') # pragma: no cover. | 348 | 348 | content = clevercsv.read_dicts(path) | | 349 | + | | 350 | + # Everything in csv is string but we try to automatically convert any numbers we find | | 351 | + for row in content: | | 352 | + for key, value in row.items(): | | 353 | + value = value.strip() | | 354 | + for type_ in [int, float, complex]: | | 355 | + try: | | 356 | + value = type_(value) | | 357 | + except Exception: | | 358 | + pass | | 359 | + else: | | 360 | + row[key] = value | | 361 | + break | 349 | 362 | else: | 350 | 363 | raise UnsupportedFormatErr(f'Only json, yaml, toml, csv, tsv and pickle are supported.\n' | 351 | 364 | f' The {file_type} extension is not known.') | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_command.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | import pytest | 3 | 3 | from shutil import copyfile | 4 | 4 | from click.testing import CliRunner | 5 | | -from deepdiff.commands import diff, deeppatch | | 5 | +from deepdiff.commands import diff, patch | 6 | 6 | from conftest import FIXTURES_DIR | 7 | 7 | from deepdiff.helper import pypy3 | 8 | 8 |
| @@ -59,7 +59,7 @@ def test_deeppatch_command(self, t1, t2, args, expected_exit_code): | 59 | 59 | the_file.write(delta_pickled.stdout_bytes) | 60 | 60 |
| 61 | 61 | runner = CliRunner() | 62 | | - deeppatched = runner.invoke(deeppatch, [t1_copy_path, delta_path]) | | 62 | + deeppatched = runner.invoke(patch, [t1_copy_path, delta_path]) | 63 | 63 | assert deeppatched.exit_code == expected_exit_code | 64 | 64 |
| 65 | 65 | runner = CliRunner() | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_diff_text.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -986,6 +986,7 @@ def test_int_to_unicode(self): | 986 | 986 | assert result == ddiff | 987 | 987 |
| 988 | 988 | @pytest.mark.parametrize("t1, t2, ignore_numeric_type_changes, significant_digits, number_format_notation, result", [ | | 989 | + (43.265798602382986, 43.71677762295505, False, 0, "f", {'values_changed': {'root': {'new_value': 43.71677762295505, 'old_value': 43.265798602382986}}}), # Note that it rounds the number so one becomes 43 and the other one is 44 | 989 | 990 | (Decimal('2.5'), Decimal('1.5'), False, 0, "f", {}), | 990 | 991 | (Decimal('2.5'), Decimal('1.5'), False, 1, "f", {'values_changed': {'root': {'new_value': Decimal('1.5'), 'old_value': Decimal('2.5')}}}), | 991 | 992 | (Decimal('2.5'), Decimal(2.5), False, 3, "f", {}), | |
|