MCPcopy
hub / github.com/treeverse/dvc / apply_diff

Function apply_diff

dvc/utils/collections.py:6–43  ·  view source on GitHub ↗

Recursively apply changes from src to dest. Preserves dest type and hidden info in dest structure, like ruamel.yaml leaves when parses files. This includes comments, ordering and line foldings. Used in Stage load/dump cycle to preserve comments and custom formatting.

(src, dest)

Source from the content-addressed store, hash-verified

4
5@no_type_check
6def apply_diff(src, dest): # noqa: C901
7 """Recursively apply changes from src to dest.
8
9 Preserves dest type and hidden info in dest structure,
10 like ruamel.yaml leaves when parses files. This includes comments,
11 ordering and line foldings.
12
13 Used in Stage load/dump cycle to preserve comments and custom formatting.
14 """
15 Seq = (list, tuple) # noqa: N806
16 Container = (Mapping, list, tuple) # noqa: N806
17
18 def is_same_type(a, b):
19 return any(
20 isinstance(a, t) and isinstance(b, t) for t in [str, Mapping, Seq, bool]
21 )
22
23 if isinstance(src, Mapping) and isinstance(dest, Mapping):
24 for key, value in src.items():
25 if isinstance(value, Container) and is_same_type(value, dest.get(key)):
26 apply_diff(value, dest[key])
27 elif key not in dest or value != dest[key]:
28 dest[key] = value
29 for key in set(dest) - set(src):
30 del dest[key]
31 elif isinstance(src, Seq) and isinstance(dest, Seq):
32 if len(src) != len(dest):
33 dest[:] = src
34 else:
35 for i, value in enumerate(src):
36 if isinstance(value, Container) and is_same_type(value, dest[i]):
37 apply_diff(value, dest[i])
38 elif value != dest[i]:
39 dest[i] = value
40 else:
41 raise AssertionError( # noqa: TRY004
42 f"Can't apply diff from {type(src).__name__} to {type(dest).__name__}"
43 )
44
45
46def to_omegaconf(item):

Callers 6

dump_datasetMethod · 0.90
_dump_pipeline_fileMethod · 0.90
to_single_stage_fileFunction · 0.90
test_apply_diff_mappingFunction · 0.90
test_apply_diff_seqFunction · 0.90

Calls 3

is_same_typeFunction · 0.85
itemsMethod · 0.80
getMethod · 0.45

Tested by 3

test_apply_diff_mappingFunction · 0.72
test_apply_diff_seqFunction · 0.72