Skip to content

Commit

Permalink
Issue #5 add included_keys arg to MultiDictGetter.simple_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenVerstraelen authored and soxofaan committed Sep 16, 2022
1 parent 0b44750 commit c3178ac
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/openeo_aggregator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,17 @@ def merge_arrays(self, key: str,skip_duplicates=False) -> list:
result.append(item)
return result

def simple_merge(self) -> dict:
def simple_merge(self, included_keys=None) -> dict:
"""
All dictionaries are merged following simple rules:
For list or sets: all elements are merged into a single list, without duplicates.
For dictionaries: all keys are added to a single dict, duplicate keys are merged recursively.
For all other types: the first value is returned.
It assumes that all duplicate keys in a dictionary have items of the same type.
Args:
included_keys: If given, only these top level keys are merged into the final dict.
"""
if len(self.dictionaries) == 0:
return {}
Expand All @@ -127,6 +130,8 @@ def simple_merge(self) -> dict:
result = {}
for dictionary in self.dictionaries:
for key, item in dictionary.items():
if included_keys is not None and key not in included_keys:
continue
if key in result:
if isinstance(item, list) or isinstance(item, set):
result[key] = self.merge_arrays(key, skip_duplicates=True)
Expand Down

0 comments on commit c3178ac

Please sign in to comment.