Predicates¶
By a predicate Satella understands a function of a single argument and a single return values. Satella’s API drastically simplifies writing lambda.
Satella lets you express lambdas in a Pythonic way, eg:
p = x == 2 assert(p(2) and not p(1)) p = x > 2 assert(p(2) and not p(1))
This behaviour extends to operators, item procurement and attr procurement. The only exceptions are operator subject to Python limitations (ie. __len__ being allowed to return an int only for example) is called.
These are mentioned in the docs below.
You can also piece together multiple predicates. Because of Python limitations please use & and | operators in place of and and or. Also use ^ in place of xor and ~ in place of not.
p = x > 2 & x < 6 assert(p(4) and not p(8) and not p(1))
PredicateClass class is documented here:
-
class
satella.coding.predicates.
PredicateClass
(operation: Callable[[Any], Any] = <function _nop>)¶ A shorthand to create lambdas using such statements, for example:
>>> add_two = x + 2 >>> assert add_two(2) == 4
-
false
() → Callable[[T], bool]¶ Return a predicate checking whether value is False
-
float
() → Callable[[T], bool]¶ Call float() on predicate
-
has
(predicate: satella.coding.predicates.PredicateClass) → Callable[[T], bool]¶ Check if any element of the current value (which must be an iterable) returns True when applied to predicate
Parameters: predicate – predicate that has to return True for at least one of this predicate’s values
-
has_keys
(*keys) → Callable[[T], bool]¶ Return a predicate checking whether this value has provided keys
-
has_p
(predicate: satella.coding.predicates.PredicateClass) → Callable[[T], bool]¶ An old name for has().
It’s deprecated. Use has() instead
Deprecated since version 2.14.22.
-
identity
() → Callable[[T], bool]¶ Spawn another object with the same operation, but different identity.
Used for constructing dicts keyed by predicates.
-
inside
(a: Callable) → Callable[[T], bool]¶ Return a predicate checking if x is inside value
-
instanceof
(a: Callable) → Callable[[T], bool]¶ Return a predicate checking whether this value is an instance of instance
-
int
() → Callable[[T], bool]¶ Call int() on predicate
-
is_instance
(*args)¶ Check if given value is one of instances.
Parameters: args – will be passed as argument to isinstance
-
is_valid_schema
(schema: Union[satella.configuration.schema.base.Descriptor, Dict[KT, VT], None] = None, **kwargs)¶ Check if given value has the correct schema. The schema is the same as in
is_valid_schema()
-
length
() → Callable[[T], bool]¶ Return a predicate returning length of its argument
-
one_of
(*values) → Callable[[T], bool]¶ Return a predicate checking if x is amongst values
-
str
() → Callable[[T], bool]¶ Call str() on predicate
-
type
() → Type[CT_co]¶ Return a predicate returning the type of it’s argument
-
To use the predicate you are to execute the following import:
from satella.coding.predicates import x p = x == 2 assert(p(2))
You can also check if a dict has provided keys
a = {'hello': 'hello', 'world': 'world'} p = x.has_keys('hello', 'world') assert p(a)
Or check whether an instance is of provided type
p = x.instanceof(int) assert p(2)
Only take care for x to be the first argument in all operators you use. For example, don’t do this:
p = 2 < x < 6
Because Python will compare first 2 with x using int’s __gt__, which will fail.
If you specify a structure, using predicates, say:
- ::
- struct = [(x, x*2)]
Then to get the corresponding {2: 4} you can use:
- ::
- a = build_structure(struct, 2, dict) assert a == {2: 4}