Skip to content

Conversation

@ZeitgeberH
Copy link

Description

Add action selection flag in the dictdiffer.patch function to allow selectively patching. For example, set action_flags='a' would only apply add items in the differ_results. See examples in the test_limit_actions_patch function in tests/test_utils.py

Checklist

Ticks in all boxes and 🟢 on all GitHub actions status checks are required to merge:

Third-party code

If you've added third-party code (copy/pasted or new dependencies), please reach out to an architect.

Reminder

By using GitHub, you have already agreed to the GitHub’s Terms of Service including that:

  1. You license your contribution under the same terms as the current repository’s license.
  2. You agree that you have the right to license your contribution under the current repository’s license.

@Napam
Copy link

Napam commented Mar 4, 2023

I would also like to have this functionality

@Napam
Copy link

Napam commented Mar 25, 2023

One could also just filter the diffs returnerd by dictdiffer.diff to only include the changes one wants, for example:

diffs = dictdiffer.diff(a, b)
diffs = [diff for diff in diffs if diff[0] == 'add'] # Only do add operations
a = dictdiffer.patch(diffs, a)

@ZeitgeberH
Copy link
Author

One could also just filter the diffs returnerd by dictdiffer.diff to only include the changes one wants, for example:

diffs = dictdiffer.diff(a, b)
diffs = [diff for diff in diffs if diff[0] == 'add'] # Only do add operations
a = dictdiffer.patch(diffs, a)

I think that may not work, as dictdiffer.diff return a generator, not a actual dict.

@Napam
Copy link

Napam commented Mar 25, 2023

One could also just filter the diffs returnerd by dictdiffer.diff to only include the changes one wants, for example:

diffs = dictdiffer.diff(a, b)
diffs = [diff for diff in diffs if diff[0] == 'add'] # Only do add operations
a = dictdiffer.patch(diffs, a)

I think that may not work, as dictdiffer.diff return a generator, not a actual dict.

I can confirm that this works. I have done this for my project and it worked fine. I don't assume that diffs is a dict, but an iterable. Here is a more complete example:

import dictdiffer

a = {'a': 1}
b = {'b': 2}
diffs = dictdiffer.diff(a, b)
diffs = [diff for diff in diffs if diff[0] == 'add']
dictdiffer.patch(diffs, a) # Results in {'a': 1, 'b': 2}

@ZeitgeberH
Copy link
Author

I see.Yes, it would work in the code example you provided.
I was thinking in more general cases in which a generator type rather than a list type is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants