Perhaps not just absolute but relative tolerance, e.g.:<div><br><div><font class="Apple-style-span" face="monospace" size="3"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 11px;"><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="border-collapse: separate; font-size: small;">def isclose(x, y, rtol=1.e-5, atol=1.e-8):</span></font></span></font></div>
<div><font class="Apple-style-span" face="monospace" size="3"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 11px;"><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="border-collapse: separate; font-size: small;"></span></font></span></font> return abs(x-y) <= atol + rtol * abs(y)</div>
<div><br></div><div><div><div class="gmail_quote">On Fri, Mar 26, 2010 at 7:59 PM, Michael Foord <span dir="ltr"><<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello all,<br>
<br>
A user has suggested an optional argument to unittest.TestCase.assertAlmostEqual for specifying a maximum difference between the expected and actual values, instead of using rounding.<br>
<br>
This sounds great to me as the default implementation of assertAlmostEqual has *never* been useful to me (YMMV). In fact one of the first things I do on setting up a test suite is provide a TestCase that overrides assertAlmostEqual with an implementation that uses a delta rather than rounding.<br>
<br>
The implementation would be effectively:<br>
<br>
assert abs(actual - expected) < delta<br>
<br>
This has the advantage that it allows things like:<br>
<br>
self.assertAlmostEqual(timeStamp, expected, delta=datetime.timedelta(seconds=5))<br>
<br>
The issue is this would make the signature of assertAlmostEqual (and its negative counterpart):<br>
<br>
def assertAlmostEqual(self, first, second, places=7, msg=None, delta=None)<br>
<br>
Note that delta comes after msg, which is different to other assert methods. To put delta before msg would be backwards incompatible with existing uses passing arguments positionally. In Python 3.2 we can make delta a keyword argument. Passing both places and delta would be an error (TypeError).<br>
<br>
Anyway, unless there are strenuous objections I intend to do this.<br>
<br>
All the best,<br>
<br>
Michael<br>
<br>
-- <br>
<a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a><br>
<a href="http://www.voidspace.org.uk/blog" target="_blank">http://www.voidspace.org.uk/blog</a><br>
<br>
READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer. <br>
<br>
<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/charles.r.mccreary%40gmail.com" target="_blank">http://mail.python.org/mailman/options/python-dev/charles.r.mccreary%40gmail.com</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Charles McCreary P.E.<br>CRM Engineering<br>903.643.3490 - office<br>903.224.5701 - mobile/GV<br>
</div></div></div>