[docs]defnegate(predicate:Predicate[T_contra])->Predicate[T_contra]:"""Negate a given predicate."""@bind_name(negate,predicate)defcheck(value:T_contra)->bool:returnnotpredicate(value)returncheck
[docs]deftruthy(value:object)->bool:"""Return :py:const:`True` for truthy objects."""returnbool(value)
[docs]deffalsy(value:object)->bool:"""Return :py:const:`True` for falsy objects."""returnnegate(truthy)(value)
[docs]defboth(p:Predicate[T_contra],q:Predicate[T_contra])->Predicate[T_contra]:""" Create a new predicate that succeeds when both of the given predicates succeed. """@bind_name(both,p,q)defcheck(value:T_contra)->bool:returnp(value)andq(value)returncheck
[docs]defeither(p:Predicate[T_contra],q:Predicate[T_contra])->Predicate[T_contra]:""" Create a new predicate that succeeds when at least one of the given predicates succeed. """@bind_name(either,p,q)defcheck(value:T_contra)->bool:returnp(value)orq(value)returncheck
[docs]defxor(p:Predicate[T_contra],q:Predicate[T_contra])->Predicate[T_contra]:""" Create a new predicate that succeeds when one of the given predicates succeed, but not both. """@bind_name(xor,p,q)defcheck(value:T_contra)->bool:returnp(value)^q(value)returncheck
[docs]defall_of(predicates:Iterable[Predicate[T_contra]])->Predicate[T_contra]:"""Create a new predicate that succeeds when all of the given predicates succeed."""predicates=tuple(predicates)@bind_name(all_of,*predicates)defcheck(value:T_contra)->bool:returnall(p(value)forpinpredicates)returncheck
[docs]defany_of(predicates:Iterable[Predicate[T_contra]])->Predicate[T_contra]:""" Create a new predicate that succeeds when at least one of the given predicates succeed. """predicates=tuple(predicates)@bind_name(any_of,*predicates)defcheck(value:T_contra)->bool:returnany(p(value)forpinpredicates)returncheck
[docs]defone_of(predicates:Iterable[Predicate[T_contra]])->Predicate[T_contra]:""" Create a new predicate that succeeds when exactly one of the given predicates succeed. """predicates=tuple(predicates)@bind_name(one_of,*predicates)defcheck(value:T_contra)->bool:returnsum(p(value)forpinpredicates)==1returncheck