Skip to content

Bugfix for significant_digits (signed zero); Numpy-friendlyness; Issue #49; Failing tests for #50; #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 30, 2016
Merged
10 changes: 7 additions & 3 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from decimal import Decimal

from collections import MutableMapping
from collections import Mapping
from collections import Iterable

from deepdiff.helper import py3, strings, numbers, ListItemRemovedOrAdded, IndexedHash, Verbose
Expand Down Expand Up @@ -640,7 +640,11 @@ def __diff_numbers(self, level):
# For Decimals, format seems to round 2.5 to 2 and 3.5 to 4 (to closest even number)
t1_s = ("{:.%sf}" % self.significant_digits).format(level.t1)
t2_s = ("{:.%sf}" % self.significant_digits).format(level.t2)
if t1_s != t2_s:

# Special case for 0: "-0.00" should compare equal to "0.00"
if set(t1_s)<=set("-0.") and set(t2_s)<=set("-0."):
return
elif t1_s != t2_s:
self.__report_result('values_changed', level)
else:
if level.t1 != level.t2:
Expand Down Expand Up @@ -668,7 +672,7 @@ def __diff(self, level, parents_ids=frozenset({})):
elif isinstance(level.t1, numbers):
self.__diff_numbers(level)

elif isinstance(level.t1, MutableMapping):
elif isinstance(level.t1, Mapping):
self.__diff_dict(level, parents_ids)

elif isinstance(level.t1, tuple):
Expand Down
5 changes: 3 additions & 2 deletions deepdiff/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,14 @@ def auto_generate_child_rel(self, klass, param):
:param param: A ChildRelationship subclass-dependent parameter describing how to get from parent to child,
e.g. the key in a dict
"""
if self.down.t1:
if self.down.t1 is not None:
self.t1_child_rel = ChildRelationship.create(klass=klass, parent=self.t1,
child=self.down.t1, param=param)
if self.down.t2:
if self.down.t2 is not None:
self.t2_child_rel = ChildRelationship.create(klass=klass, parent=self.t2,
child=self.down.t2, param=param)


@property
def all_up(self):
"""
Expand Down
51 changes: 51 additions & 0 deletions tests/test_diff_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def test_same_objects(self):
res = ddiff.tree
self.assertEqual(res, {})

def test_significant_digits_signed_zero(self):
t1 = 0.00001
t2 = -0.0001
ddiff = DeepDiff(t1, t2, significant_digits = 2)
res = ddiff.tree
self.assertEqual(res, {})
t1 = 1*10**-12
t2 = -1*10**-12
ddiff = DeepDiff(t1, t2, significant_digits=10)
res = ddiff.tree
self.assertEqual(res, {})


def test_item_added_extensive(self):
t1 = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
t2 = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'new': 1337}
Expand Down Expand Up @@ -108,6 +121,7 @@ def test_non_subscriptable_iterable(self):
self.assertIsInstance(change.up.t1_child_rel, NonSubscriptableIterableRelationship)
self.assertIsNone(change.up.t2_child_rel)


def test_non_subscriptable_iterable_path(self):
t1 = (i for i in [42, 1337, 31337])
t2 = (i for i in [42, 1337, ])
Expand All @@ -118,3 +132,40 @@ def test_non_subscriptable_iterable_path(self):
self.assertEqual(change.path(), None)
self.assertEqual(change.path(force='yes'), 'root(unrepresentable)')
self.assertEqual(change.path(force='fake'), 'root[2]')

def test_significant_digits(self):
ddiff = DeepDiff([0.012, 0.98], [0.013, 0.99], significant_digits = 1)
self.assertEqual(ddiff, {})

@unittest.expectedFailure
def test_significant_digits_with_sets(self):
ddiff = DeepDiff(set([0.012, 0.98]), set([0.013, 0.99]), significant_digits = 1)
self.assertEqual(ddiff, {})

@unittest.expectedFailure
def test_significant_digits_with_ignore_order(self):
ddiff = DeepDiff([0.012, 0.98], [0.013, 0.99], significant_digits = 1, ignore_order=True)
self.assertEqual(ddiff, {})

class DeepDiffRefWithNumpyTestCase(unittest.TestCase):

"""DeepDiff Tests."""
def setUp(self):
import numpy as np
a1 = np.array([1.23, 1.66, 1.98])
a2 = np.array([1.23, 1.66, 1.98])
self.d1 = { 'np': a1 }
self.d2 = { 'np': a2 }

def test_diff_with_numpy(self):
ddiff = DeepDiff(self.d1, self.d2)
res = ddiff.tree
self.assertEqual(res, {})

def test_diff_with_empty_seq(self):
a1 = {"empty":[]}
a2 = {"empty":[]}
ddiff = DeepDiff(a1, a2)
res = ddiff.tree
self.assertEqual(ddiff, {})