dictdiffer icon indicating copy to clipboard operation
dictdiffer copied to clipboard

action selection flag when patching

Open ZeitgeberH opened this issue 3 years ago • 5 comments

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.

ZeitgeberH avatar Jan 04 '23 20:01 ZeitgeberH

I would also like to have this functionality

Napam avatar Mar 04 '23 23:03 Napam

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)

Napam avatar Mar 25 '23 12:03 Napam

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.

ZeitgeberH avatar Mar 25 '23 16:03 ZeitgeberH

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}

Napam avatar Mar 25 '23 17:03 Napam

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.

ZeitgeberH avatar Mar 25 '23 18:03 ZeitgeberH