Predicates and factories¶
Predicates are functions that return a boolean value given a single argument. These modules contain predicate functions, and functions that return predicates, that can be composed and used for phantom types.
Boolean logic¶
-
phantom.predicates.boolean.
true
(_value)[source]¶ Always return
True
.- Parameters
_value (
object
) –- Return type
typing.Literal
[True]
-
phantom.predicates.boolean.
false
(_value)[source]¶ Always return
False
.- Parameters
_value (
object
) –- Return type
typing.Literal
[False]
-
phantom.predicates.boolean.
negate
(predicate)[source]¶ Negate a given predicate.
- Parameters
predicate (
typing.Callable
[[-T],bool
]) –- Return type
typing.Callable
[[-T],bool
]
-
phantom.predicates.boolean.
truthy
(value)[source]¶ Return
True
for truthy objects.- Parameters
value (
object
) –- Return type
bool
-
phantom.predicates.boolean.
falsy
(value)[source]¶ Return
True
for falsy objects.- Parameters
value (
object
) –- Return type
bool
-
phantom.predicates.boolean.
both
(p, q)[source]¶ Create a new predicate that succeeds when both of the given predicates succeed.
- Parameters
p (
typing.Callable
[[-T],bool
]) –q (
typing.Callable
[[-T],bool
]) –
- Return type
typing.Callable
[[-T],bool
]
-
phantom.predicates.boolean.
either
(p, q)[source]¶ Create a new predicate that succeeds when at least one of the given predicates succeed.
- Parameters
p (
typing.Callable
[[-T],bool
]) –q (
typing.Callable
[[-T],bool
]) –
- Return type
typing.Callable
[[-T],bool
]
-
phantom.predicates.boolean.
xor
(p, q)[source]¶ Create a new predicate that succeeds when one of the given predicates succeed, but not both.
- Parameters
p (
typing.Callable
[[-T],bool
]) –q (
typing.Callable
[[-T],bool
]) –
- Return type
typing.Callable
[[-T],bool
]
-
phantom.predicates.boolean.
all_of
(predicates)[source]¶ Create a new predicate that succeeds when all of the given predicates succeed.
- Parameters
predicates (
typing.Iterable
[typing.Callable
[[-T],bool
]]) –- Return type
typing.Callable
[[-T],bool
]
Collection¶
-
phantom.predicates.collection.
contains
(value)[source]¶ Create a new predicate that succeeds when its argument contains
value
.- Parameters
value (
object
) –- Return type
typing.Callable
[[Container
],bool
]
-
phantom.predicates.collection.
contained
(container)[source]¶ Create a new predicate that succeeds when its argument is contained by
container
.- Parameters
container (
typing.Container
) –- Return type
typing.Callable
[[object
],bool
]
-
phantom.predicates.collection.
count
(predicate)[source]¶ Create a predicate that succeeds when the size of its argument satisfies the given
predicate
.- Parameters
predicate (
typing.Callable
[[int
],bool
]) –- Return type
typing.Callable
[[Sized
],bool
]
Datetime¶
Generic¶
-
phantom.predicates.generic.
equal
(a)[source]¶ Create a new predicate that succeeds when its argument is equal to
a
.- Parameters
a (
object
) –- Return type
typing.Callable
[[object
],bool
]
Numeric intervals¶
Functions that create new predicates that succeed when their argument is strictly or non
strictly between the upper and lower bounds. There are corresponding phantom types that
use these predicates in phantom.interval
.
-
phantom.predicates.interval.
open
(low, high)[source]¶ Create a predicate that succeeds when its argument is in the range
(low, high)
.- Parameters
low (
float
) –high (
float
) –
- Return type
typing.Callable
[[float
],bool
]
-
phantom.predicates.interval.
open_closed
(low, high)[source]¶ Create a predicate that succeeds when its argument is in the range
(low, high]
.- Parameters
low (
float
) –high (
float
) –
- Return type
typing.Callable
[[float
],bool
]
Numeric¶
-
phantom.predicates.numeric.
less
(n)[source]¶ Create a new predicate that succeeds when its argument is strictly less than
n
.- Parameters
n (
float
) –- Return type
typing.Callable
[[float
],bool
]
-
phantom.predicates.numeric.
le
(n)[source]¶ Create a new predicate that succeeds when its argument is less than or equal to
n
.- Parameters
n (
float
) –- Return type
typing.Callable
[[float
],bool
]
-
phantom.predicates.numeric.
greater
(n)[source]¶ Create a new predicate that succeeds when its argument is strictly greater than
n
.- Parameters
n (
float
) –- Return type
typing.Callable
[[float
],bool
]
-
phantom.predicates.numeric.
ge
(n)[source]¶ Create a new predicate that succeeds when its argument is greater than or equal to
n
.- Parameters
n (
float
) –- Return type
typing.Callable
[[float
],bool
]
-
phantom.predicates.numeric.
positive
(n)[source]¶ Return
True
whenn
is strictly greater than zero.- Parameters
n (
float
) –- Return type
bool
-
phantom.predicates.numeric.
non_positive
(n)[source]¶ Return
True
whenn
is less than or equal to zero.- Parameters
n (
float
) –- Return type
bool
-
phantom.predicates.numeric.
negative
(n)[source]¶ Return
True
whenn
is strictly less than zero.- Parameters
n (
float
) –- Return type
bool
-
phantom.predicates.numeric.
non_negative
(n)[source]¶ Return
True
whenn
is greater than or equal to zero.- Parameters
n (
float
) –- Return type
bool
-
phantom.predicates.numeric.
modulo
(n, p)[source]¶ Create a new predicate that succeeds when its argument modulo
n
satisfies the given predicatep
.- Parameters
n (
float
) –p (
typing.Callable
[[float
],bool
]) –
- Return type
typing.Callable
[[float
],bool
]