Standard predicates
This section describes the predicates that are included in py-predicate.
Set predicates
A collection of predicates that act on sets.
in_p
Return True if the values are included in the set, otherwise False.
from predicate import in_p
in_123 = in_p(1, 2, 3)
assert not in_123(0)
assert in_123(1)
is_subset_p
Return True if the value is a subset, otherwise False.
from predicate import is_subset_p
sub_123 = is_subset_p({1, 2, 3})
assert not sub_123({0, 1})
assert sub_123({1, 2})
is_superset_p
Return True if the value is a superset, otherwise False.
from predicate import is_superset_p
super_123 = is_superset_p({1, 2, 3})
assert not super_123({1, 2, 4})
assert super_123({1, 2, 3, 4})
is_real_subset_p
Return True if the value is a real subset, otherwise False.
from predicate import is_real_subset_p
sub_123 = is_real_subset_p({1, 2, 3})
assert not sub_123({1, 2, 3})
assert sub_123({1, 2})
is_real_superset_p
Return True if the value is a real superset, otherwise False.
from predicate import is_real_superset_p
super_123 = is_real_superset_p({1, 2, 3})
assert not super_123({1, 2, 3})
assert super_123({1, 2, 3, 4})
not_in_p
Return True if the values are not in the set, otherwise False.
from predicate import not_in_p
not_in_123 = not_in_p(1, 2, 3)
assert not_in_123(0)
assert not not_in_123(1)
String predicates
A collection of predicates that act on strings.
is_alnum_p
from predicate import is_alnum_p
is_alpha_p
from predicate import is_alpha_p
is_ascii_p
from predicate import is_ascii_p
is_decimal_p
from predicate import is_decimal_p
is_digit_p
from predicate import is_digit_p
is_identifier_p
from predicate import is_identifier_p
is_lower_p
from predicate import is_lower_p
is_numeric_p
from predicate import is_numeric_p
is_printable_p
from predicate import is_printable_p
is_space_p
from predicate import is_space_p
is_title_p
from predicate import is_title_p
is_upper_p
from predicate import is_upper_p
all_p
This predicate tests if for all elements in an Iterable, the enclosed predicate is True:
from predicate import all_p, is_int_p
# Predicate to test if all items in an iterable are of type int
all_int = all_p(is_int_p)
assert all_int([1, 2, 3])
assert not all_int([None, 2, 3])
The equivalent code without using predicates could be something like:
def all_int(iter: Iterable) -> bool:
return all(isinstance(ele, int) for ele in iter)
always_false_p
This predicate ignores any arguments and always returns False:
from predicate import always_false_p
assert not always_false_p(13)
This might be the result of an optimization.
always_true_p
This predicate ignores any arguments and always returns True:
from predicate import always_true_p
assert always_true_p(13)
This might be the result of an optimization.
any_p
This predicate tests if for any element in an Iterable, the enclosed predicate is True:
from predicate import any_p, is_int_p
# Predicate to test if any of the items in an iterable is of type int
any_int = any_p(is_int_p)
assert not any_int(())
assert any_int((1, 2, 3))
assert any_int([1, 2, 3])
assert any_int([None, 2, 3])
eq_false_p
This predicates tests if the argument is False.
from predicate import eq_false_p
assert eq_false_p(False)
assert not eq_false_p(True)
Note that this tests for the exact boolean value False. If you want to test for falsy values, see: is_falsy_p.
eq_p
This predicates tests for equality.
from predicate import eq_p
eq_2 = eq_p(2)
assert eq_2(2)
assert not eq_2(3)
eq_true_p
This predicates tests if the argument is True.
from predicate import eq_true_p
assert not eq_true_p(False)
assert eq_true_p(True)
Note that this tests for the exact boolean value True. If you want to test for falsy values, see: is_truthy_p.
fn_p
This predicate can be used to wrap any (lambda) function:
from predicate import fn_p
square_ge_2 = fn_p(lambda x: x * x >= 2)
assert not square_ge_2(1)
assert square_ge_2(2)
ge_p
This predicates tests for greater or equal a value.
from predicate import ge_p
ge_2 = ge_p(2)
assert ge_2(2)
assert not ge_2(1)
gt_p
This predicates tests for greater than a value.
from predicate import gt_p
gt_2 = gt_p(2)
assert not gt_2(2)
assert gt_2(3)
is_bool_p
This predicate tests if the value is of type bool
. Only True if the value is either False
or True
.
from predicate import is_bool_p
assert is_bool_p(False)
assert is_bool_p(True)
is_callable_p
This predicate tests if the value is of type Callable
.
from predicate import is_callable_p
assert is_callable(is_callable)
assert is_callable(lambda x: x)
assert is_callable(str.upper)
is_complex_p
This predicate tests if the value is of type complex
.
from predicate import is_complex_p
assert not is_complex(1)
assert is_complex(complex(1, 1))
is_container_p
This predicate tests if the value is of type Container
.
from predicate import is_container_p
assert is_container_p((1, 2, 3))
assert is_container_p([1, 2, 3])
assert is_container_p({1, 2, 3})
assert is_container_p({"one": 1, "two": 2, "three": 3})
assert is_container_p("one") # a string is also a container!
is_datetime_p
This predicate tests if the value is of type datetime
.
from predicate import is_datetime_p
is_dict_p
This predicate tests if the value is of type dict
.
from predicate import is_dict_p
assert is_dict_p({"one": 1, "two": 2, "three": 3})
is_dict_of_p
This predicate tests if the value is of type dict
and the key and values match the predicates.
from predicate import is_dict_of_p, is_int_p, eq_p
# test for dictionaries that have keys x and y. The values should be integers
predicate = is_dict_of_p((eq_p("x"), is_int_p), (eq_p("y"), is_int_p))
assert predicate({"x": 1, "y": 7})
is_empty_p
This predicate tests if an iterable is empty.
from predicate import is_empty_p
assert is_empty_p(())
assert is_empty_p({})
assert is_empty_p([])
assert is_empty_p("")
is_falsy_p
This predicate tests for falsy values, for example False, “”, {}, [], 0, etc.
from predicate import is_falsy_p
assert is_falsy_p(0)
assert is_falsy_p({})
is_finite_p
from predicate import is_finite_p
is_float_p
This predicate tests if the value is of type float
.
from predicate import is_float_p
assert not is_float_p(3)
assert is_float_p(3.14)
is_hashable_p
This predicate tests if the value is hashable.
from predicate import is_hashable_p
assert not is_hashable_p({})
assert is_hashable_p("foo")
is_inf_p
This predicate tests if the value is infinite.
import math
from predicate import is_inf_p
assert not is_inf_p(3)
assert is_inf_p(math.inf)
is_instance_p
from predicate import is_instance_p
is_int_p
This predicate tests if the value is of type int
.
from predicate import is_int_p
assert not is_int_p(3.14)
assert is_int_p(3)
is_iterable_of_p
from predicate import is_iterable_of_p
is_iterable_p
This predicate tests if the value is of type Iterable
.
from predicate import is_iterable_p
is_list_of_p
This predicate predicate tests if the value if a list, where all items in the list conform to a given predicate.
from predicate import is_list_of_p, is_str_p
is_list_of_str = is_list_of_p(is_str_p)
assert not is_list_of_str(["one", "two", 3])
assert is_list_of_str(["foo"])
is_list_p
This predicate tests if the value is of type list
.
from predicate import is_list_p
assert not is_list_p({1, 2, 3})
assert is_list_p([1, 2, 3])
is_none_p
from predicate import is_none_p
assert not is_none_p(13)
assert is_none_p(None)
is_not_none_p
from predicate import is_not_none_p
assert not is_not_none_p(None)
assert is_not_none_p(13)
is_predicate_p
This predicate tests if the value is of type Predicate
.
from predicate import is_predicate_p
is_range_p
This predicate tests if value is a range.
from predicate import is_range_p
assert not is_range_p(0)
assert is_range_p(range(5))
is_set_of_p
This predicate predicate tests if the value if a set, where all items in the set conform to a given predicate.
from predicate import is_set_of_p, is_str_p
is_set_of_str = is_set_of_p(is_str_p)
assert not is_set_of_str({"one", "two", 3})
assert is_set_of_str({"foo"})
is_set_p
This predicate tests if the value is of type set
.
from predicate import is_set_p
is_str_p
This predicate tests if the value is of type str
.
from predicate import is_str_p
is_truthy_p
This predicate tests for truthy values, for example True, “foo”, {“foo”}, [1], 13, etc.
from predicate import is_truthy_p
assert is_truthy_p(1)
assert is_truthy_p({"foo"})
is_tuple_of_p
from predicate import is_tuple_of_p
is_tuple_p
This predicate tests if the value is of type tuple
.
from predicate import is_tuple_p
is_uuid_p
This predicate tests if the value is of type uuid
.
from predicate import is_uuid_p
le_p
This predicates tests for less than or equal a value.
from predicate import le_p
le_2 = le_p(2)
assert le_2(2)
assert not le_2(3)
lt_p
This predicates tests for less than a value.
from predicate import lt_p
lt_2 = lt_p(2)
assert not lt_2(2)
assert lt_2(1)
ne_p
This predicate tests for non equality
from predicate import ne_p
ne_2 = ne_p(2)
assert not ne_2(2)
assert ne_2(3)
tee_p
Predicate that always returns True, but is useful for handling side-effects.
from predicate import all_p, lt_p, tee_p
log = tee_p(print)
all_lt_2 = all_p(log | lt_p(2))