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=<function _nop>)¶
A shorthand to create lambdas using such statements, for example:
>>> add_two = x + 2 >>> assert add_two(2) == 4
- Parameters:
operation (Callable[[Any], Any]) –
- false()¶
Return a predicate checking whether value is False
- Return type:
Callable[[T], bool]
- float()¶
Call float() on predicate
- Return type:
Callable[[T], bool]
- has(predicate)¶
Check if any element of the current value (which must be an iterable) returns True when applied to predicate
- Parameters:
predicate (PredicateClass) – predicate that has to return True for at least one of this predicate’s values
- Return type:
Callable[[T], bool]
- has_keys(*keys)¶
Return a predicate checking whether this value has provided keys
- Return type:
Callable[[T], bool]
- has_p(predicate)¶
An old name for has().
It’s deprecated. Use has() instead
Deprecated since version 2.14.22.
- Parameters:
predicate (PredicateClass) –
- Return type:
Callable[[T], bool]
- identity()¶
Spawn another object with the same operation, but different identity.
Used for constructing dicts keyed by predicates.
- Return type:
Callable[[T], bool]
- inside(a)¶
Return a predicate checking if x is inside value
- Parameters:
a (Callable) –
- Return type:
Callable[[T], bool]
- instanceof(a)¶
Return a predicate checking whether this value is an instance of instance
- Parameters:
a (Callable) –
- Return type:
Callable[[T], bool]
- int()¶
Call int() on predicate
- Return type:
Callable[[T], bool]
- is_instance(*args)¶
Check if given value is one of instances.
- Parameters:
args – will be passed as argument to isinstance
- is_valid_schema(schema=None, **kwargs)¶
Check if given value has the correct schema. The schema is the same as in
is_valid_schema()
- Parameters:
schema (Optional[Union[Descriptor, Dict]]) –
- length()¶
Return a predicate returning length of its argument
- Return type:
Callable[[T], bool]
- one_of(*values)¶
Return a predicate checking if x is amongst values
- Return type:
Callable[[T], bool]
- str()¶
Call str() on predicate
- Return type:
Callable[[T], bool]
- type()¶
Return a predicate returning the type of it’s argument
- Return type:
Type
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}