deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

how to access the other data from value changed node easily with deepdiff?

Open NewUserHa opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

import deepdiff
deepdiff.DeepDiff({'foo':['data1', 'data2', 'data3'], 'bar': None},{'foo':['data1', 'data2', 'data4', 'bar': None]})

return

{'values_changed': {"root['foo'][2]": {'new_value': 3, 'old_value': 2}}}

when using deepdiff to compare the dicts, and want to re-access the 'data2', the current method is to use regex to match the "root['foo'][2]" to get the parent path. is it possible to expose some attributes for the case?

Describe the solution you'd like maybe the deepdiff.diff.DeepDiff has a new attribute like d.value_changed_key?

NewUserHa avatar May 04 '22 07:05 NewUserHa

Hi, Have you looked into the tree view?

https://zepworks.com/deepdiff/current/view.html#tree-view

Sep Dehpour

On May 4, 2022, at 12:38 AM, NewUserHa @.***> wrote:

 Is your feature request related to a problem? Please describe.

import deepdiff deepdiff.DeepDiff({'foo':['data1', 'data2', 'data3'], 'bar': None},{'foo':['data1', 'data2', 'data4', 'bar': None]}) return

{'values_changed': {"root['foo'][2]": {'new_value': 3, 'old_value': 2}}} when using deepdiff to compare the dicts, and want to re-access the 'data2', the current method is to use regex to match the "root['foo'][2]" to get the parent path. is it possible to expose some attributes for the case?

Describe the solution you'd like maybe the deepdiff.diff.DeepDiff has a new attribute like d.value_changed_key?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

seperman avatar May 04 '22 14:05 seperman

ok, it seems forgot can use:

import deepdiff
d=deepdiff.DeepDiff({'foo':['data1', 'data2', 'data3'], 'bar': None},{'foo':['data1', 'data2', 'data4', 'bar': None]},view='tree')
d['values_changed'][0].up.t1[1] # or t2

to re-access 'data2'.

but it was that I wanted to get the 'foo' (instead of 'bar') then re-access manually. and it seems a good method too. is it possible to get the key/path of the node changed like 'foo' too?

NewUserHa avatar May 04 '22 14:05 NewUserHa

Hi @NewUserHa

Here is the example:

In [11]: t1={'foo':['data1', 'data2', 'data3'], 'bar': None}

In [12]: t2={'foo':['data1', 'data2', 'data4'], 'bar': None}

In [16]: diff=DeepDiff(t1, t2, view='tree')

In [17]: diff
Out[17]: {'values_changed': [<root['foo'][2] t1:'data3', t2:'data4'>]}

In [18]: diff['values_changed'][0]
Out[18]: <root['foo'][2] t1:'data3', t2:'data4'>

In [19]: diff['values_changed'][0].t1
Out[19]: 'data3'

In [20]: diff['values_changed'][0].t2
Out[20]: 'data4'

In [21]: diff['values_changed'][0].path()
Out[21]: "root['foo'][2]"

In [23]: diff['values_changed'][0].path(output_format='list')
Out[23]: ['foo', 2]

seperman avatar Nov 19 '23 15:11 seperman