-
Notifications
You must be signed in to change notification settings - Fork 28
IMPROVE merge_2_dicts #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,23 +89,33 @@ def merge_2_dicts(dict1, dict2, value_merge_func=None): | |
| if dict1 is None: | ||
| return dict2 | ||
|
|
||
| def merged_value(key): | ||
| if key not in dict1: | ||
| return dict2[key] | ||
| elif key not in dict2: | ||
| return dict1[key] | ||
| else: | ||
| if value_merge_func is None: | ||
| raise ValueError( | ||
| "Conflict in merged dictionaries: merge function not " | ||
| "provided but key {} exists in both dictionaries".format( | ||
| key)) | ||
|
|
||
| return value_merge_func(dict1[key], dict2[key]) | ||
|
|
||
| keys = set(dict1.keys()) | set(dict2.keys()) | ||
|
|
||
| return {key: merged_value(key) for key in keys} | ||
| if dict1 == dict2: | ||
| for k, v in dict1.items(): | ||
| dict1[k] = value_merge_func(v, v) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the merge function is for example |
||
| return dict1 | ||
|
|
||
| dict1_set = set(dict1) | ||
| dict2_set = set(dict2) | ||
|
|
||
| keys_to_merge = dict1_set.intersection(dict2_set) | ||
|
|
||
| if len(keys_to_merge) == 0 and value_merge_func is None: | ||
|
||
| raise ValueError( | ||
| "Conflict in merged dictionaries: merge function not " | ||
| "provided but keys {} exists in both dictionaries".format( | ||
| keys_to_merge)) | ||
|
|
||
| values_merged = dict() | ||
|
|
||
| for key_to_merge in keys_to_merge: | ||
| old_value1 = dict1.pop(key_to_merge) | ||
| old_value2 = dict2.pop(key_to_merge) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless really proven to be necessary for optimisations reasons, I would discourage mutating I think that thanks to the order you put in We obtain the equivalent logic. What do you think ? |
||
| new_value = value_merge_func(old_value1, old_value2) | ||
|
|
||
| values_merged[key_to_merge] = new_value | ||
|
|
||
| return {**dict1, **dict2, **values_merged} | ||
|
|
||
|
|
||
| def df_concat(d1, d2): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the addition of
Outgoing, that indeed clarifies the direction.However I would not use the term
relationshipin this class since that term has a specific meaning in the context of Trumania => I think that would cause a confusion betweenRelationshipandOutgoingRelationship(maybe suggesting the later is a specific case of the former)=> how about
OutgoingRelations?