Skip to content
Snippets Groups Projects

Added the possibility to use custom labels instead of 'old' and 'new'

Merged Alexander Schlemmer requested to merge f-improve-diff-descriptions into dev
All threads resolved!
Files
4
+ 53
11
@@ -512,24 +512,63 @@ def merge_entities(entity_a: Entity,
return entity_a
def describe_diff(olddiff, newdiff, name=None, as_update=True):
def describe_diff(olddiff: dict[str, Any], newdiff: dict[str, Any],
name: str = None,
as_update: bool = True,
label_old: str = "old version",
label_new: str = "new version") -> str:
"""
Generate a textual description of the differences between two entities.
These can be generated using :func:`compare_entities` and used within this function like this:
`describe_diff(*compare_entities(...))`
Arguments:
----------
olddiff: dict[str, Any]
First element of the tuple output of :func:`compare_entities`.
This is referred to as the "old" version.
newdiff: dict[str, Any]
Second element of the tuple output of :func:`compare_entities`.
This is referred to as the "new" version.
name: str
Default None. Name of the entity that will be shown in the output text.
as_update: bool
Default True. Not used anymore.
label_old: str
Can be used to set a custom label for the diff that is associated with the old entity.
label_new: str
Can be used to set a custom label for the diff that is associated with the new entity.
Returns:
--------
A text description of the differences.
"""
description = ""
for attr in list(set(list(olddiff.keys()) + list(newdiff.keys()))):
if attr == "parents" or attr == "properties":
continue
description += "{} differs:\n".format(attr)
description += "old version: {}\n".format(
description += label_old + ": {}\n".format(
olddiff[attr] if attr in olddiff else "not set")
description += "new version: {}\n\n".format(
description += label_new + ": {}\n\n".format(
newdiff[attr] if attr in newdiff else "not set")
if len(olddiff["parents"]) > 0:
description += ("Parents that are only in the old version:\n"
description += ("Parents that are only in the " + label_old + ":\n"
+ ", ".join(olddiff["parents"]) + "\n")
if len(newdiff["parents"]) > 0:
description += ("Parents that are only in the new version:\n"
description += ("Parents that are only in the " + label_new + ":\n"
+ ", ".join(olddiff["parents"]) + "\n")
for prop in list(set(list(olddiff["properties"].keys())
@@ -537,18 +576,21 @@ def describe_diff(olddiff, newdiff, name=None, as_update=True):
description += "property {} differs:\n".format(prop)
if prop not in olddiff["properties"]:
description += "it does not exist in the old version: \n"
description += "it does not exist in the " + label_old + ":\n"
elif prop not in newdiff["properties"]:
description += "it does not exist in the new version: \n"
description += "it does not exist in the " + label_new + ":\n"
else:
description += "old version: {}\n".format(
description += label_old + ": {}\n".format(
olddiff["properties"][prop])
description += "new version: {}\n\n".format(
description += label_new + ": {}\n\n".format(
newdiff["properties"][prop])
if description != "":
description = ("## Difference between the old and the new "
"version of {}\n\n".format(name))+description
description = ("## Difference between the " +
label_old +
" and the " +
label_new +
" of {}\n\n".format(name)) + description
return description
Loading