jump to navigation

Python’s mock.call and floats June 27, 2017

Posted by PythonGuy in Uncategorized.
add a comment

You may have seen an error like this:

AssertionError: [call(0.2),
 call(3.3000000000000003)] != [call(0.2), call(0.3), call(0.5), call(0.9), call(1.7), call(3.3)]

This is one of the problems with comparing floats in Python (or any language, for that matter.)

You need to do some rounding if you want to compare floats in a meaningful way.

Try this instead:

for (actual, expected) in zip(
        (0.2, 0.3, 0.5, 0.9, 1.7, 3.3)):
    self.assertAlmostEqual(actual[0][0], expected)

It’s a burden. If you need to do this a lot, then you can write your own comparator function.

Python Isn’t Why Your Code is Slow June 13, 2017

Posted by PythonGuy in Uncategorized.
add a comment

Oftentimes, we blame our programming language for the slowness of our programs.

A recent thread in /r/programming shows that it’s not the language — it’s the program.

With a little work, you can make a Python “yes” that is almost as fast as GNU’s “yes”.

The less, kiddos, is that when your program is slow, it’s your program, not the tools. Now, the tools might make things harder, but your job is to write the best program, so you have to work around that.