Skip to content

Instantly share code, notes, and snippets.

@samuraisam
Last active March 29, 2024 22:17
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save samuraisam/901117 to your computer and use it in GitHub Desktop.
Save samuraisam/901117 to your computer and use it in GitHub Desktop.
Deep Equality Test for Nested Python Structures
#Copyright (c) 2010-2013 Samuel Sutch [samuel.sutch@gmail.com]
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
import datetime, time, functools, operator, types
default_fudge = datetime.timedelta(seconds=0, microseconds=0, days=0)
def deep_eq(_v1, _v2, datetime_fudge=default_fudge, _assert=False):
"""
Tests for deep equality between two python data structures recursing
into sub-structures if necessary. Works with all python types including
iterators and generators. This function was dreampt up to test API responses
but could be used for anything. Be careful. With deeply nested structures
you may blow the stack.
Options:
datetime_fudge => this is a datetime.timedelta object which, when
comparing dates, will accept values that differ
by the number of seconds specified
_assert => passing yes for this will raise an assertion error
when values do not match, instead of returning
false (very useful in combination with pdb)
Doctests included:
>>> x1, y1 = ({'a': 'b'}, {'a': 'b'})
>>> deep_eq(x1, y1)
True
>>> x2, y2 = ({'a': 'b'}, {'b': 'a'})
>>> deep_eq(x2, y2)
False
>>> x3, y3 = ({'a': {'b': 'c'}}, {'a': {'b': 'c'}})
>>> deep_eq(x3, y3)
True
>>> x4, y4 = ({'c': 't', 'a': {'b': 'c'}}, {'a': {'b': 'n'}, 'c': 't'})
>>> deep_eq(x4, y4)
False
>>> x5, y5 = ({'a': [1,2,3]}, {'a': [1,2,3]})
>>> deep_eq(x5, y5)
True
>>> x6, y6 = ({'a': [1,'b',8]}, {'a': [2,'b',8]})
>>> deep_eq(x6, y6)
False
>>> x7, y7 = ('a', 'a')
>>> deep_eq(x7, y7)
True
>>> x8, y8 = (['p','n',['asdf']], ['p','n',['asdf']])
>>> deep_eq(x8, y8)
True
>>> x9, y9 = (['p','n',['asdf',['omg']]], ['p', 'n', ['asdf',['nowai']]])
>>> deep_eq(x9, y9)
False
>>> x10, y10 = (1, 2)
>>> deep_eq(x10, y10)
False
>>> deep_eq((str(p) for p in xrange(10)), (str(p) for p in xrange(10)))
True
>>> str(deep_eq(range(4), range(4)))
'True'
>>> deep_eq(xrange(100), xrange(100))
True
>>> deep_eq(xrange(2), xrange(5))
False
>>> import datetime
>>> from datetime import datetime as dt
>>> d1, d2 = (dt.now(), dt.now() + datetime.timedelta(seconds=4))
>>> deep_eq(d1, d2)
False
>>> deep_eq(d1, d2, datetime_fudge=datetime.timedelta(seconds=5))
True
"""
_deep_eq = functools.partial(deep_eq, datetime_fudge=datetime_fudge,
_assert=_assert)
def _check_assert(R, a, b, reason=''):
if _assert and not R:
assert 0, "an assertion has failed in deep_eq (%s) %s != %s" % (
reason, str(a), str(b))
return R
def _deep_dict_eq(d1, d2):
k1, k2 = (sorted(d1.keys()), sorted(d2.keys()))
if k1 != k2: # keys should be exactly equal
return _check_assert(False, k1, k2, "keys")
return _check_assert(operator.eq(sum(_deep_eq(d1[k], d2[k])
for k in k1),
len(k1)), d1, d2, "dictionaries")
def _deep_iter_eq(l1, l2):
if len(l1) != len(l2):
return _check_assert(False, l1, l2, "lengths")
return _check_assert(operator.eq(sum(_deep_eq(v1, v2)
for v1, v2 in zip(l1, l2)),
len(l1)), l1, l2, "iterables")
def op(a, b):
_op = operator.eq
if type(a) == datetime.datetime and type(b) == datetime.datetime:
s = datetime_fudge.seconds
t1, t2 = (time.mktime(a.timetuple()), time.mktime(b.timetuple()))
l = t1 - t2
l = -l if l > 0 else l
return _check_assert((-s if s > 0 else s) <= l, a, b, "dates")
return _check_assert(_op(a, b), a, b, "values")
c1, c2 = (_v1, _v2)
# guard against strings because they are iterable and their
# elements yield iterables infinitely.
# I N C E P T I O N
for t in types.StringTypes:
if isinstance(_v1, t):
break
else:
if isinstance(_v1, types.DictType):
op = _deep_dict_eq
else:
try:
c1, c2 = (list(iter(_v1)), list(iter(_v2)))
except TypeError:
c1, c2 = _v1, _v2
else:
op = _deep_iter_eq
return op(c1, c2)
@kkurian
Copy link

kkurian commented Aug 26, 2013

Love this Gist. :)

Little improvement: Calls like deep_eq({'foo': 'bar'}, None) result in an AttributeError.

To fix this, replace line 99 with the following:

    try:
        k1, k2 = (sorted(d1.keys()), sorted(d2.keys()))
    except AttributeError:
        return False

@zags
Copy link

zags commented Oct 7, 2013

Suggested improvement on the last piece of code (line 129-143):

    for t in types.StringTypes:
        if isinstance(_v1, t):
            return op(c1, c2)

    if isinstance(_v1, types.DictType):
        return _deep_dict_eq(c1, c2)

    try:
        c1, c2 = (list(iter(_v1)), list(iter(_v2)))
    except TypeError:
        c1, c2 = _v1, _v2
    else:
        return _deep_iter_eq(c1, c2)

    return op(c1, c2)

@vmeyet
Copy link

vmeyet commented Nov 13, 2013

suggested improvement (129-131).

Why doing a for loop:

for t in types.StringTypes:
    if isinstance(_v1, t):
      break

when not needed, the following work just fine:

if isinstance(_v1,  types.StringTypes):
  break

@seanwoodward
Copy link

also be careful of circular references

@blairg23
Copy link

These two dictionaries are equal but are not considered equal by this module:

toolchain = {'children': [{'id': '11', 'target': None},
{'id': '12', 'target': '11'},
{'id': '5', 'target': '7'},
{'id': '6', 'target': '5'},
{'id': '7', 'target': '8'},
{'id': '8', 'target': '12'},
{'id': '3', 'target': '6'},
{'id': '1', 'target': '2'},
{'id': '2', 'target': '3'}],
'macrotool_id': '38'}

toolchain_fixed = {'children': [{'id': '1', 'target': '2'},
{'id': '2', 'target': '3'},
{'id': '3', 'target': '6'},
{'id': '6', 'target': '5'},
{'id': '5', 'target': '7'},
{'id': '7', 'target': '8'},
{'id': '8', 'target': '12'},
{'id': '12', 'target': '11'},
{'id': '11', 'target': None}],
'macrotool_id': '38'}

I think the main problem is that these dictionaries contain nested lists, which also contain dictionaries nested in them. This exposes a simple bug in the program: cannot compare two Python data structures that have nested data structures differing from the original data structure inside of them. Although this is a simple bug, it is a complex bug to fix and may not be done easily.

@sellonen
Copy link

@blairg23 I don't see why those should be equal. The children are in different order, and order is relevant in a list.

@The-Compiler
Copy link

...why not simply use ==, which does the same except for the generators and datetime_fudge?

@Yuanduo
Copy link

Yuanduo commented May 3, 2016

Love this Gist.
Can you build it to a package and upload to PyPI? Then people who also love it do not need to copy it for their code.

@valericus
Copy link

valericus commented Jun 23, 2016

Yes, I'm agree with Yuando, package on pypi is great idea. If you wish I can help you with wrapping your code to package.

@marscher
Copy link

marscher commented Jan 9, 2018

Has somebody forked this yet to enable NumPy support?

@baali
Copy link

baali commented Aug 3, 2018

I tried using it with python3 and types don't have StringTypes anymore and while looking for a fix, I came across StackOverflow conversations and blogs mentioning that comparing type is not a good idea and instead we should try duck typing to figure out type of those variables, something like:

try:
    _v1.title()
except TypeError:
    try:
        _v1.items()
        op = _deep_dict_eq
    except TypeError:
        try:
            c1, c2 = (list(iter(_v1)), list(iter(_v2)))
        except TypeError:
            c1, c2 = _v1, _v2
        else:
            op = _deep_iter_eq  

@aauss
Copy link

aauss commented Nov 16, 2018

If you want it to make it work on Python 3 with the minimum amount of changes, change line 129 such that

 for t in [str]:
    if isinstance(_v1, t):
      break
  else:

@worldmind
Copy link

Is it something like deepdiff?

@iamsarthakjoshi
Copy link

Thanks much!

If someone having trouble with these:
AttributeError: module 'types' has no attribute 'StringTypes'
AttributeError: module 'types' has no attribute 'DictType'

Just change these lines for the fix:

    for t in [str]: **// this - replace types.StringTypes to [str]**
        if isinstance(_v1, t):
            break
        else:
            if isinstance(_v1, dict):  **// this - replace types.DictType to dict**

Cheers!

@tuf22191
Copy link

Lol so many changes - anybody have the final version?

@atsepkov
Copy link

Only a couple changes needed:

s/types.StringTypes/[str]/
s/types.DictType/dict/

The original version overcomplicated the types and broke in python3 as a result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment