check almost equals in deeply nested dicts with arrays (please read the desc)

Issue

suppose I got two nested dicts with arrays of dicts inside, i want to check if the values are close enough.
== doesnt work since it doesnt check for array values
extending the ApproxMapping class doesnt work either

dict1 = {
'a': 1, 
'b': [
      {'c': [{'d': 32.069},{'e': 32.420}]}
     ]
}
dict2 = {
'a': 1, 
'b': [
      {'c': [{'d': 32.070},{'e': 32.421}]}
     ]
}

How can I check they are almost equal, is there anyway i can override the pytest.approx method to work for nested dicts and arrays?

Solution

Checkout the deepdiff library:

from deepdiff import DeepDiff

dict1 = {
'a': 1, 
'b': [
      {'c': [{'d': 32.069},{'e': 32.420}]}
     ]
}
dict2 = {
'a': 1, 
'b': [
      {'c': [{'d': 32.070},{'e': 32.421}]}
     ]
}

diff = DeepDiff(dict1, dict2, significant_digits=2)
print(diff) # {}

Answered By – Andrey Ilin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published