deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Allow ignore some expected differences

Open ntkathole opened this issue 5 years ago • 3 comments

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

I have certain expected changes in 2 dicts but deepdiff throws error for such changes. Is their any way to ignore some expected changes?

In [1]: x                                                                                                                                        
Out[1]: [{'date': 'June 14', 'data': {'fedora': 'no data', 'centos': '0'}}]

In [2]: y                                                                                                                                        
Out[2]: [{'date': 'June 14', 'data': {'fedora': '0', 'centos': '0'}}]

In [3]: diff = deepdiff.DeepDiff(x, y)                                                                                                           

In [4]: print(json.dumps(json.loads(diff.to_json()), indent=4))                                                                                  
{
    "values_changed": {
        "root[0]['data']['fedora']": {
            "new_value": "0",
            "old_value": "no data"
        }
    }
}

Describe the solution you'd like

I expect some features such as diff = deepdiff.DeepDiff(x, y, ignore_expected_in_groups=[(0, "no data")])

Describe alternatives you've considered Replacing "no data" with 0 from output to assert.

In [1]: x                                                                                                                                        
Out[1]: [{'date': 'June 14', 'data': {'fedora': 'no data', 'centos': '0'}}]

In [2]: for x1 in x: 
    ...:     for x2,x3 in x1['data'].items(): 
    ...:         if x3 == "no data": 
    ...:             x1['data'][x2]=0 
    ...:                                                                                                                                          

In [3]: x                                                                                                                                        
Out[3]: [{'date': 'June 14', 'data': {'fedora': 0, 'centos': '0'}}]

Additional context I am not sure if there is something simpler or some existing way to achieve this.

ntkathole avatar Jun 14 '20 05:06 ntkathole

Hi @ntkathole Have you looked into exclude_paths ? You can tell DeepDiff to ignore any changes for those variables via this parameter.

seperman avatar Jun 20 '20 04:06 seperman

Thanks @seperman for looking into this. exclude_paths completely remove that variable from comparison. I am looking for ignoring particular value of the key.

In [1]: x = [{'date': 'June 14', 'data': {'fedora': '4', 'centos': '0'}}]

In [2]: y = [{'date': 'June 14', 'data': {'fedora': '0', 'centos': '0'}}]

In [3]: deepdiff.DeepDiff(x, y, exclude_paths=["root[0]['data']['fedora']"])                                                                     
Out[3]: {}

ntkathole avatar Jun 20 '20 04:06 ntkathole

I see. DeepDiff currently doesn't expose such a capability.

seperman avatar Jun 20 '20 04:06 seperman