This would basically be for matching on floats when precision isn't controlled, although theoretically it'll work on any type that implements subtraction and abs():
class Nearly(object):
"""Within a certain threshold."""
def __init__(self, expected, epsilon=0.001):
self.expected = expected
self.epsilon = epsilon
def __str__(self):
return 'Nearly(%r, %r)' % (self.expected, self.epsilon)
def match(self, value):
if abs(value - self.expected) > self.epsilon:
return Mismatch(
u'%r more than %r from %r' % (
value, self.epsilon, self.expected))
This would basically be for matching on floats when precision isn't controlled, although theoretically it'll work on any type that implements subtraction and abs():