lkj 0.1.40__tar.gz → 0.1.42__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lkj-0.1.40 → lkj-0.1.42}/PKG-INFO +1 -1
- {lkj-0.1.40 → lkj-0.1.42}/lkj/__init__.py +1 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/dicts.py +16 -11
- {lkj-0.1.40 → lkj-0.1.42}/lkj.egg-info/PKG-INFO +1 -1
- {lkj-0.1.40 → lkj-0.1.42}/setup.cfg +1 -1
- {lkj-0.1.40 → lkj-0.1.42}/LICENSE +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/README.md +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/chunking.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/filesys.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/funcs.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/importing.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/iterables.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/loggers.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/misc.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj/strings.py +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj.egg-info/SOURCES.txt +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj.egg-info/dependency_links.txt +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj.egg-info/not-zip-safe +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/lkj.egg-info/top_level.txt +0 -0
- {lkj-0.1.40 → lkj-0.1.42}/setup.py +0 -0
|
@@ -13,6 +13,7 @@ from lkj.dicts import (
|
|
|
13
13
|
inclusive_subdict, # new dictionary with only the keys in `include`
|
|
14
14
|
exclusive_subdict, # new dictionary with only the keys not in `exclude`.
|
|
15
15
|
merge_dicts, # Merge multiple dictionaries recursively
|
|
16
|
+
compare_field_values, # Compare two dictionaries' values
|
|
16
17
|
)
|
|
17
18
|
from lkj.filesys import get_app_data_dir, get_watermarked_dir, enable_sourcing_from_file
|
|
18
19
|
from lkj.strings import (
|
|
@@ -224,7 +224,11 @@ Comparison = Any
|
|
|
224
224
|
Comparator = Callable[[dict, dict], Comparison]
|
|
225
225
|
|
|
226
226
|
|
|
227
|
-
def
|
|
227
|
+
def _common_keys_list(dict1, dict2):
|
|
228
|
+
return [k for k in dict1.keys() if k in dict2.keys()]
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def compare_field_values(
|
|
228
232
|
dict1,
|
|
229
233
|
dict2,
|
|
230
234
|
*,
|
|
@@ -232,16 +236,17 @@ def compare_dicts(
|
|
|
232
236
|
default_comparator: Comparator = operator.eq,
|
|
233
237
|
aggregator: Callable[
|
|
234
238
|
[Dict[KT, Comparison]], Any
|
|
235
|
-
] = lambda d: d, # lambda d: np.mean(list(d.values()))
|
|
239
|
+
] = lambda d: d, # lambda d: np.mean(list(d.values())),
|
|
240
|
+
get_comparison_fields: Callable[[dict], Iterable[KT]] = _common_keys_list,
|
|
236
241
|
):
|
|
237
242
|
"""
|
|
238
|
-
Compare two dictionaries field by field
|
|
243
|
+
Compare two dictionaries' values field by field
|
|
239
244
|
|
|
240
245
|
:param dict1: The first dictionary.
|
|
241
246
|
:param dict2: The second dictionary.
|
|
242
247
|
:param field_comparators: A dictionary where keys are field names and values are comparator functions.
|
|
243
248
|
:param default_comparator: A default comparator function to use if no specific comparator is provided for a field.
|
|
244
|
-
:param aggregator: A function to aggregate the comparison results into a final
|
|
249
|
+
:param aggregator: A function to aggregate the comparison results into a final comparison object.
|
|
245
250
|
:return: A final score based on the comparison results.
|
|
246
251
|
|
|
247
252
|
>>> dict1 = {"color": "brown", "animal": "dog"}
|
|
@@ -251,22 +256,22 @@ def compare_dicts(
|
|
|
251
256
|
... "color": lambda x, y: 1 if x == y else 0,
|
|
252
257
|
... "animal": lambda x, y: 1 if len(x) == len(y) else 0
|
|
253
258
|
... }
|
|
254
|
-
>>>
|
|
259
|
+
>>> compare_field_values(dict1, dict2, field_comparators=field_comparators)
|
|
255
260
|
{'color': 1, 'animal': 1}
|
|
256
|
-
>>>
|
|
261
|
+
>>> compare_field_values(dict1, dict3, field_comparators=field_comparators)
|
|
257
262
|
{'color': 1, 'animal': 0}
|
|
258
263
|
>>> import functools, statistics
|
|
259
264
|
>>> aggregator = lambda d: statistics.mean(d.values())
|
|
260
|
-
>>>
|
|
261
|
-
...
|
|
265
|
+
>>> mean_of_values = functools.partial(
|
|
266
|
+
... compare_field_values, field_comparators=field_comparators, aggregator=aggregator
|
|
262
267
|
... )
|
|
263
|
-
>>>
|
|
268
|
+
>>> mean_of_values(dict1, dict2)
|
|
264
269
|
1
|
|
265
|
-
>>>
|
|
270
|
+
>>> mean_of_values(dict1, dict3)
|
|
266
271
|
0.5
|
|
267
272
|
|
|
268
273
|
"""
|
|
269
|
-
common_keys =
|
|
274
|
+
common_keys = get_comparison_fields(dict1, dict2)
|
|
270
275
|
|
|
271
276
|
comparisons = {}
|
|
272
277
|
for key in common_keys:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|