action selection flag when patching
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:
- [x] I'm aware of the code of conduct.
- [x] I've created logical separate commits and followed the commit message format.
- [x] I've added relevant test cases.
- [x] I've added relevant documentation.
- [x] I've marked translation strings (for relevant code).
- [x] I've followed the CSS/JS and React guidelines (for relevant code).
- [x] I've followed the web accessibility guidelines (for relevant code).
- [x] I've followed the user interface guidelines (for relevant code).
- [x] I've identified the copyright holder(s) and updated copyright headers for touched files (>15 lines contributions).
- [x] I've NOT included third-party code (copy/pasted source code or new dependencies).
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:
- You license your contribution under the same terms as the current repository’s license.
- You agree that you have the right to license your contribution under the current repository’s license.
I would also like to have this functionality
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)
One could also just filter the diffs returnerd by
dictdiffer.diffto 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.
One could also just filter the diffs returnerd by
dictdiffer.diffto 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.diffreturn 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}
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.