-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.py
More file actions
46 lines (40 loc) · 1.4 KB
/
Copy pathdiff.py
File metadata and controls
46 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import typing
from diff import json_path
from diff.delta import Delta
def diff(new: dict[str, typing.Any], old: dict[str, typing.Any]) -> list[Delta]:
new_path_map = json_path.path_value_map(
new, include_root=True, leaves_only=True, include_containers=False
)
old_path_map = json_path.path_value_map(
old, include_root=True, leaves_only=False, include_containers=False
)
operations: list[Delta] = []
deleted = old_path_map.keys() - new_path_map.keys()
for key in deleted:
operations.append( # noqa: PERF401
Delta(
path=key,
operation="deleted",
old_value=old_path_map[key],
new_value=None,
)
)
added = new_path_map.keys() - old_path_map.keys()
for key in added:
operations.append( # noqa: PERF401
Delta(
path=key, operation="added", old_value=None, new_value=new_path_map[key]
)
)
shared_keys = new_path_map.keys() & old_path_map.keys()
for key in shared_keys:
if old_path_map[key] != new_path_map[key]:
operations.append( # noqa: PERF401
Delta(
path=key,
operation="modified",
old_value=old_path_map[key],
new_value=new_path_map[key],
)
)
return operations