Skip to content

Commit 3291c4d

Browse files
author
Michael Fritzsche
committed
formatting and doc strings
1 parent ac36a28 commit 3291c4d

2 files changed

Lines changed: 58 additions & 121 deletions

File tree

src/hermes/model/merge/match.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,77 @@ def match_func(left: Any, right: Any) -> bool:
5858

5959

6060
def match_person(left: Any, right: Any) -> bool:
61+
"""
62+
Compares two objects assuming they are representing schema:Person's
63+
if they are not ld_dicts, == is used as a fallback.<br>
64+
If both objects have an @id value, the truth value returned by this function is the comparison of both ids.
65+
If either other has no @id value and both objects have at least one email value,
66+
they are considered equal if they have one common email.
67+
If the equality of the objects is not yet decided, == comparison of the objects is returned.
68+
69+
:param left: The first object for the comparison.
70+
:type left: ld_merge_dict
71+
:param right: The second object for the comparison.
72+
:type right: ld_dict
73+
74+
:return: The result of the comparison.
75+
:rtype: bool
76+
"""
6177
if not (isinstance(left, ld_dict) and isinstance(right, ld_dict)):
6278
return left == right
6379
if "@id" in left and "@id" in right:
6480
return left["@id"] == right["@id"]
6581
if "schema:email" in left and "schema:email" in right:
66-
mails_right = right["schema:email"]
67-
return any((mail in mails_right) for mail in left["schema:email"])
82+
if len(left["schema:email"]) > 0 and len(right["schema:email"]) > 0:
83+
mails_right = right["schema:email"]
84+
return any((mail in mails_right) for mail in left["schema:email"])
6885
return left == right
6986

7087

7188
def match_multiple_types(
7289
*functions_for_types: list[tuple[str, Callable[[Any, Any], bool]]],
7390
fall_back_function: Callable[[Any, Any], bool] = match_keys("@id", fall_back_to_equals=True)
7491
) -> Callable[[Any, Any], bool]:
92+
"""
93+
Returns a function that compares two objects using the given functions.
94+
95+
:param functions_for_types: Tuples of type and match_function.
96+
The returned function will compare two objects of a the same, given type with the specified function.
97+
:type functions_for_types: list[tuple[str, Callable[[Any, Any], bool]]]
98+
:param fall_back_function: The fallback for comparison if the objects that are being compared don't have a common
99+
type with specified compare function or at least one object is not a JSON-LD dictionary.
100+
:type fall_back_function: Callable[[Any, Any], bool]
101+
102+
:return: The function that compares the two given objects using the given functions.
103+
:rtype: Callable[[Any, Any], bool]
104+
"""
105+
106+
# create and return the match function using the given keys
75107
def match_func(left: Any, right: Any) -> bool:
76-
if not ((isinstance(left, ld_dict) and isinstance(right, ld_dict)) and "@type" in left and "@type" in right):
108+
"""
109+
Compares two objects using a predetermined function if either objects is not an ld_dict
110+
or they don't have a common type in a predetermined list of types.<br>
111+
If the objects are ld_dicts and have the same type with a known comparison function this is used instead.
112+
113+
:param left: The first object for the comparison.
114+
:type left: ld_merge_dict
115+
:param right: The second object for the comparison.
116+
:type right: ld_dict
117+
118+
:return: The result of the comparison.
119+
:rtype: bool
120+
"""
121+
# If at least one of the objects is not an ld_dict or contains no value for the key "@type", use the fallback.
122+
if not (isinstance(left, ld_dict) and isinstance(right, ld_dict) and "@type" in left and "@type" in right):
77123
return fall_back_function(left, right)
124+
# Extract the list of types
78125
types_left = left["@type"]
79126
types_right = right["@type"]
127+
# Iterate over all known type, match_function pairs.
128+
# If one type is in both objects return the result of the comparison with the match_function.
80129
for ld_type, func in functions_for_types:
81130
if ld_type in types_left and ld_type in types_right:
82131
return func(left, right)
132+
# No common type with known match_function: Fallback
83133
return fall_back_function(left, right)
84134
return match_func

0 commit comments

Comments
 (0)