From 2cfc6edb4c1e5c97357ebc6acbe047765232cbf3 Mon Sep 17 00:00:00 2001 From: Daniel Pressler Date: Tue, 6 Jun 2023 07:56:30 -0700 Subject: [PATCH] make consistency.Approx repr consistent with class behavior --- sarpy/consistency/consistency.py | 15 ++++++++------- tests/consistency/test_consistency.py | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/sarpy/consistency/consistency.py b/sarpy/consistency/consistency.py index f06d577e..77d94fc7 100644 --- a/sarpy/consistency/consistency.py +++ b/sarpy/consistency/consistency.py @@ -418,25 +418,26 @@ def __lt__(self, rhs): return self.__le__(rhs) def __le__(self, rhs): - return np.all(np.logical_or(np.less(self.value, rhs), - np.isclose(self.value, rhs, rtol=self.rtol, atol=self.atol))) + return np.all(np.logical_or(np.less(self.value, rhs), self._isclose(rhs))) def __eq__(self, rhs): - return np.all(np.isclose(self.value, rhs, rtol=self.rtol, atol=self.atol)) + return np.all(self._isclose(rhs)) def __ne__(self, rhs): return not self.__eq__(rhs) def __ge__(self, rhs): - return np.all(np.logical_or(np.greater(self.value, rhs), - np.isclose(self.value, rhs, rtol=self.rtol, atol=self.atol))) + return np.all(np.logical_or(np.greater(self.value, rhs), self._isclose(rhs))) def __gt__(self, rhs): return self.__ge__(rhs) def __repr__(self): - tol = np.maximum(self.atol, np.asarray(self.value) * self.rtol) - return "{} +/- {}".format(self.value, tol) + tol = self.atol + np.abs(np.asarray(self.value)) * self.rtol + return f"{self.value} ± {tol}" + + def _isclose(self, rhs): + return np.isclose(rhs, self.value, rtol=self.rtol, atol=self.atol) def in_color(string, *color): diff --git a/tests/consistency/test_consistency.py b/tests/consistency/test_consistency.py index 2fd86b34..57f3ad28 100644 --- a/tests/consistency/test_consistency.py +++ b/tests/consistency/test_consistency.py @@ -161,7 +161,7 @@ def test_invalid(dummycon): def test_approx(): - apx = con.Approx(10.0, atol=.1) + apx = con.Approx(10.0, atol=.1, rtol=0) assert apx == 10.0 assert apx == 10.01 assert not apx != 10.01 @@ -171,4 +171,4 @@ def test_approx(): assert not apx <= 0 assert apx < 10.01 assert apx <= 10.01 - assert repr(apx) == "10.0 +/- 0.1" + assert repr(apx) == "10.0 ± 0.1"