seperman / deepdiff

DeepDiff: Deep Difference and search of any Python object/data. DeepHash: Hash of any object based on its contents. Delta: Use deltas to reconstruct objects by adding deltas together.

Home Page:http://zepworks.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: How is the index determined for iterables when using `ignore_order=True`?

raven42 opened this issue · comments

When performing a DeepDiff operation with ignore_order=True on two objects that have an iterable in them how is the index printed determined? From basic testing, it looks as if for common items is it guaranteed to use the index from t1? Then for added items will it always use t2? and such?

For example:

t1 = {
    'values': [
        {
            'name': 'item1',
            'value': 'someValue'
        },
        {
            'name': 'item2',
            'value': 'anotherValue'
        }
    ]
}

t2 = {
    'values': [
        {
            'name': 'item2',
            'value': 'changedValue'
        },
        {
            'name': 'item3',
            'value': 'newItem'
        },
        {
            'name': 'item1',
            'value': 'someValue'
        }
    ]
}
ddiff = DeepDiff(t1, t2, ignore_order=True)
print(json.dumps(ddiff, indent=2))

This results in:

{
  "values_changed": {
    "root['values'][1]['value']": {
      "new_value": "changedValue",
      "old_value": "anotherValue"
    }
  },
  "iterable_item_added": {
    "root['values'][1]": {
      "name": "item3",
      "value": "newItem"
    }
  }
}

So specifically in the line "root['values'][1]['value']": { I'm looking for a predictable way to identify the [1] index in the path so I can add additional code to perform an actual lookup on the corresponding object (similarly in the "root['values'][1]": { line).

I couldn't find anything in the corresponding documentation that discusses this.

If you need to parse the path, you are better off with the tree view:
https://zepworks.com/deepdiff/current/view.html#tree-view

In terms of where the path comes from, it is trying to tell you what modifications to the first object will turn it into the second object. So in your example, it is first modifying root['values'][1]['value'] and then inserting another item at root['values'][1].

I agree with you that it is not clearly described in the docs.

Ok thanks. I was able to switch to the tree-view and that did help get the info I needed.

Thanks!