Code Monkey home page Code Monkey logo

Comments (3)

seperman avatar seperman commented on May 29, 2024

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.

tpcalhoun avatar tpcalhoun commented on May 29, 2024

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.

tpcalhoun avatar tpcalhoun commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.