How to Validate Negative Matches¶
Sometimes you want to check that data is not equal to a specific value. There are a few different ways to perform this type of negative matching.
Helper Function¶
One obvious way to check for a negative match is to define a helper
function that checks for !=
to a given value:
1 2 3 4 5 6 7 8 | from datatest import validate
data = [...]
def not_bar(x):
return x != 'bar'
validate(data, not_bar)
|
Inverted Predicate¶
Datatest provides a Predicate
class for handling different
kinds of matching. You can invert a Predicate’s behavior using the
inversion operator, ~
:
1 2 3 4 | from datatest import validate, Predicate
data = [...]
validate(data, ~Predicate('bar'))
|
Functional Style¶
If you are accustomed to programming in a functional style, you
could perform a negative match using functools.partial()
and
operator.ne()
:
1 2 3 4 5 6 | from functools import partial
from operator import ne
from datatest import validate
data = [...]
validate(data, partial(ne, 'bar'))
|