Comments (3)
Hi @tpcalhoun
The reason you are running into this issue is that you are passing updates.to_dict()
to Delta instead of the diff object itself.
You can either serialize the DeepDiff object, then recreate the DeepDiff object and pass it to Delta to recreate Delta, or you can create the Delta object and serialize Delta itself and restore Delta from the serialized Delta.
diff = DeepDiff(dict1, dict2)
delta = Delta(diff)
Serialization options are documented here: https://zepworks.com/deepdiff/current/serialization.html
from deepdiff.
From: Sep
Sent: Tuesday, December 12, 2023 1:45 PM
To: Todd
Subject: RE: [EXTERNAL] Re: DeepDiff errors
If you want to have them pass the updates as a flat list of dictionaries, you can avoid DeepDiff and go straight to Delta:
https://zepworks.com/deepdiff/current/serialization.html#delta-load-from-flat-dictionaries
---- On Tue, 12 Dec 2023 11:34:08 -0800 Todd wrote ---
Thank you for the quick reply.
Ok, I have not tried your suggestion yet but…..
My upstream team is going to send a single JSON like:
event = {
“beforeImage”: {“key”: “value”},
“updates”: {“key”: “value”}
}
So I would need to generate my “afterImage”.
Sort of like this:
afterimage = event.get(‘beforeImage’) + Delta(event.get(‘updates’))
Is there a way for me to convert my “updates” JSON (python dictionary) to a “diff object” to pass into my Delta? In short, I will not be using the DeepDiff class normally, just the Delta class.
Please reply at your convenience.
Todd
---- On Tue, 12 Dec 2023 12:57 PM Sep wrote ---
From: Sep
Sent: Tuesday, December 12, 2023 12:57 PM
To: Todd
Subject: [EXTERNAL] Re: DeepDiff errors
Importance: High
Hi Todd,
Closed your ticket on Github. Please read my comments.
Thanks,
Sep
from deepdiff.
THANK YOU SEP. Your suggestion worked for me, see corrected code examples below:
def test_demonstrate_corrected_dictionary_item_removed(): """ Demonstrate fail with: {'dictionary_item_removed': [root['individualCustomer']['employment']]} """ dict1 = {"individualCustomer": { "gender": "Male", "citizenship": { "country": "United States" }, "employment": { "employerName": "ABC LTD", "jobTitle": "Bookkeeper", "status": "Full-time" } }} # Make a copy of dict1 dict2 = copy.deepcopy(dict1) # Remove the inner object employment from dict2 only. dict2['individualCustomer'].pop('employment') # find the difference between the two dictionaries # putting dict1 FIRST... diff = DeepDiff(dict1, dict2) delta = Delta(diff) flat_dict_list = delta.to_flat_dicts() # Note this flat_dict_list is what we need to pass as event['updates'] updates = flat_dict_list # ----------- Simulate processing event received --------------- event = {"beforeImage": dict1, "updates": updates} # now using a Delta and beforeImage, try to generate afterImage delta = Delta(flat_dict_list=event.get('updates')) after_image = event.get('beforeImage') + delta # -------------------------------------------------------------- final_diff = DeepDiff(dict2, after_image) assert final_diff == {} assert dict2 == after_image def test_demonstrate_corrected_dictionary_item_added(): """ Demonstrate fail with: {'dictionary_item_added': [root['individualCustomer']['employment']]} """ dict1 = {"individualCustomer": { "gender": "Male", "citizenship": { "country": "United States" }, "employment": { "employerName": "ABC LTD", "jobTitle": "Bookkeeper", "status": "Full-time" } }} # Make a copy of dict1 dict2 = copy.deepcopy(dict1) # Remove the inner object employment from dict2 only. dict1['individualCustomer'].pop('employment') # find the difference between the two dictionaries # putting dict2 FIRST... diff = DeepDiff(dict1, dict2) delta = Delta(diff) flat_dict_list = delta.to_flat_dicts() # Note this flat_dict_list is what we need to pass as event['updates'] updates = flat_dict_list # ----------- Simulate processing event received --------------- event = {"beforeImage": dict1, "updates": updates} # now using a Delta and beforeImage, try to generate afterImage delta = Delta(flat_dict_list=event.get('updates')) after_image = event.get('beforeImage') + delta # -------------------------------------------------------------- final_diff = DeepDiff(dict2, after_image) assert final_diff == {} assert dict2 == after_image
from deepdiff.
Related Issues (20)
- 6.4.0 importing deepdiff results in none type error HOT 5
- Simple typo in test paths generation. HOT 1
- Delta.__add__ breaks with esoteric dict keys HOT 1
- Function `parse_path` doesn't exist but is referenced in the docs. HOT 1
- Keys containing "__a__" at the beginning will be ignored. HOT 1
- Diff does not work for attributes of a class that inherits from dict or collections.UserDict HOT 4
- DeepDiff raises decimal exception when using significant digits HOT 3
- Can't compare dicts with both single and double quotes in keys HOT 3
- Inconsistent Behavior with math_epsilon and ignore_order HOT 1
- max_diffs doesn't use ignore_nan_inequality flag
- Wrong diff on list of strings HOT 3
- DeepDiff fails when dictionary keys are unrepresentable and exclude_regex_paths is not None
- Error on Delta With None Key and Removed Item from List
- Need to be able to see full path on all outputs HOT 1
- Error when subtracting Delta from a dictionary HOT 4
- Documentation on how to consume keys HOT 2
- Error after comparing: Can not produce a hash (type datetime.date doesn't define __round__ method) HOT 1
- parse_path() cannot parse a path HOT 4
- Is there a way to systematically cycle through all corresponding attributes that haven't changed? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepdiff.