Make a few stylistic improvements.

This commit is contained in:
Kale Kundert 2016-03-07 10:09:20 -08:00
parent 6f5e1e386a
commit dd28e28b34
2 changed files with 14 additions and 8 deletions

View File

@ -1349,8 +1349,8 @@ class approx:
>>> 0.1 + 0.2 == 0.3 >>> 0.1 + 0.2 == 0.3
False False
This problem is commonly encountered when writing tests, e.g. to make sure This problem is commonly encountered when writing tests, e.g. when making
that a floating-point function returns the expected values. The best way sure that a floating-point function returns the expected values. One way
to deal with this problem is to assert that two floating point numbers are to deal with this problem is to assert that two floating point numbers are
equal to within some appropriate margin:: equal to within some appropriate margin::
@ -1399,6 +1399,8 @@ class approx:
>>> 0.1 + 0.2 == approx(0.3, abs=1e-100) >>> 0.1 + 0.2 == approx(0.3, abs=1e-100)
False False
>>> 0.1 + 0.2 == approx(0.3, rel=1e-6, abs=1e-100)
True
""" """
def __init__(self, expected, rel=None, abs=None): def __init__(self, expected, rel=None, abs=None):
@ -1408,24 +1410,24 @@ class approx:
def __repr__(self): def __repr__(self):
from collections import Iterable from collections import Iterable
plus_minus = lambda x: '{}\u00B1{}'.format(x, self._margin(x)) plus_minus = lambda x: '{} \u00B1 {:.1e}'.format(x, self._get_margin(x))
if isinstance(self.expected, Iterable): if isinstance(self.expected, Iterable):
return str([plus_minus(x) for x in self.expected]) return str([plus_minus(x) for x in self.expected])
else: else:
plus_minus(self.expected) return plus_minus(self.expected)
def __eq__(self, actual): def __eq__(self, actual):
from collections import Iterable from collections import Iterable
expected = self.expected expected = self.expected
almost_eq = lambda a, x: abs(x - a) < self._margin(x) almost_eq = lambda a, x: abs(x - a) < self._get_margin(x)
if isinstance(actual, Iterable) and isinstance(expected, Iterable): if isinstance(actual, Iterable) and isinstance(expected, Iterable):
return all(almost_eq(a, x) for a, x in zip(actual, expected)) return all(almost_eq(a, x) for a, x in zip(actual, expected))
else: else:
return almost_eq(actual, expected) return almost_eq(actual, expected)
def _margin(self, x): def _get_margin(self, x):
margin = self.max_absolute_error or 1e-100 margin = self.max_absolute_error or 1e-100
if self.max_relative_error is None: if self.max_relative_error is None:
@ -1435,7 +1437,6 @@ class approx:
return max(margin, x * (self.max_relative_error or 1e-6)) return max(margin, x * (self.max_relative_error or 1e-6))
# #
# the basic pytest Function item # the basic pytest Function item
# #

View File

@ -1,3 +1,5 @@
# encoding: utf-8
import pytest import pytest
import doctest import doctest
@ -13,7 +15,7 @@ class MyDocTestRunner(doctest.DocTestRunner):
class TestApprox: class TestApprox:
def test_approx(self): def test_approx_doctests(self):
parser = doctest.DocTestParser() parser = doctest.DocTestParser()
test = parser.get_doctest( test = parser.get_doctest(
pytest.approx.__doc__, pytest.approx.__doc__,
@ -24,4 +26,7 @@ class TestApprox:
runner = MyDocTestRunner() runner = MyDocTestRunner()
runner.run(test) runner.run(test)
def test_repr_string(self):
print(pytest.approx(1.0))
assert repr(pytest.approx(1.0)) == '1.0 ± 1.0e-06'