brennivin.pyobjcomparer module

Module for comparing arbitrary python object structures using the compare() function.

Uses a map of type to comparison method to perform comparisons recursively.

This module can be pretty easily refactored so that the comparison methods, tolerance, etc., are customizable.

This module is necessary so we can customize the built-in behavior of python’s structure comparer, which very almost suits our needs, except for comparison of floats and similar.

Members

brennivin.pyobjcomparer.assert_compare(a, b, print_objs=True)

Raise AssertionError if a != b, else return None.

brennivin.pyobjcomparer.compare(a, b)

Returns True if a equals b using the special sauce logic, False if not.

Return type:bool
brennivin.pyobjcomparer.get_compound_diff(a, b)

If a != b return list of compound elements from a that leads to the differing value.

If a == b return empty list.

>>> a = [{'first': [{'second': 0}]}]
>>> b = [{'first': [{'second': 1}]}]
>>> get_compound_diff(a, b)
[0, 'first', 0, 'second']

An edge-case exists where a != b, but the point of inequality occurs immediately, without traversing into the input structures (e.g. if one or both inputs are not lists or dicts). In this case the returned list becomes [a, b].

>>> get_compound_diff(object(), 'foo')  
[<object object at 0x...>, 'foo']
Return type:list