gallery.accords-library.com/server/szurubooru/func/diff.py

63 lines
1.9 KiB
Python
Raw Normal View History

from typing import List, Dict, Any
def get_list_diff(old: List[Any], new: List[Any]) -> Any:
2016-08-14 18:06:49 +00:00
equal = True
removed = [] # type: List[Any]
added = [] # type: List[Any]
2016-08-14 18:06:49 +00:00
for item in old:
if item not in new:
equal = False
removed.append(item)
2016-08-14 18:06:49 +00:00
for item in new:
if item not in old:
equal = False
added.append(item)
2016-08-14 18:06:49 +00:00
return None if equal else {
'type': 'list change', 'added': added, 'removed': removed}
2016-08-14 18:06:49 +00:00
def get_dict_diff(old: Dict[str, Any], new: Dict[str, Any]) -> Any:
2016-08-14 18:06:49 +00:00
value = {}
equal = True
for key in old.keys():
if key in new:
if old[key] != new[key]:
if isinstance(old[key], dict) and isinstance(new[key], dict):
value_diff = get_dict_diff(old[key], new[key])
if value_diff:
equal = False
value[key] = value_diff
elif isinstance(old[key], list) and isinstance(new[key], list):
value_diff = get_list_diff(old[key], new[key])
if value_diff:
equal = False
value[key] = value_diff
else:
equal = False
value[key] = {
'type': 'primitive change',
'old-value': old[key],
'new-value': new[key],
}
else:
equal = False
value[key] = {
'type': 'deleted property',
'value': old[key]
}
for key in new.keys():
if key not in old:
equal = False
value[key] = {
'type': 'added property',
'value': new[key],
}
return None if equal else {'type': 'object change', 'value': value}