Skip to content
Snippets Groups Projects
Commit e1b3dfc6 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

2.10.0

parent 8d21fb85
No related branches found
Tags v2.10.0
No related merge requests found
......@@ -5,6 +5,8 @@ Predicates
Predicates are functions that take something and return a boolean about truthfulness
of given statement. Satella contains a bunch of functions to produce these predicates.
These go superbly hand-in-hand with preconditions and postconditions.
Predicates
----------
......@@ -43,6 +45,15 @@ Decorators are used to extend given predicates. Eg:
p = [1, 2, 5]
assert item(equals(2), 1)(p)
::
p = [1, 2, 5]
assert p_all(item(equals(1), 0), item(equals(2), 1))
::
p = [1, 2, 5]
assert p_any(item(equals(1), 0), item(equals(2), 1))
.. autofunction:: satella.coding.predicates.attribute
.. autofunction:: satella.coding.predicates.item
__version__ = '2.10.0_b8'
__version__ = '2.10.0'
from .number import between
from .generic import one_of, equals, is_not_none, not_equal, has_attr
from .sequences import length_is, length_multiple_of, shorter_than, longer_than
from .decorators import attribute, item
from .decorators import attribute, item, p_all, p_any
__all__ = ['between', 'one_of', 'length_is', 'shorter_than', 'length_multiple_of',
'equals', 'attribute', 'item', 'is_not_none', 'not_equal', 'longer_than',
'has_attr']
'has_attr', 'p_all', 'p_any']
import typing as tp
def p_all(*args: tp.Callable[[tp.Any], bool]) -> tp.Callable[[tp.Any], bool]:
"""
Make a predicate returning True if all specified predicates return True
"""
def predicate(v):
return all(arg(v) for arg in args)
return predicate
def p_any(*args: tp.Callable[[tp.Any], bool]) -> tp.Callable[[tp.Any], bool]:
"""
Make a predicate returning True if any of specified predicates return True
"""
def predicate(v):
return any(arg(v) for arg in args)
return predicate
def attribute(p: tp.Callable[[tp.Any], bool], attr) -> tp.Callable[[tp.Any], bool]:
"""
Make predicate p refer to attribute of the object passed to it.
......
......@@ -3,11 +3,21 @@ import unittest
from satella.coding.predicates import between, one_of, length_is, shorter_than, \
length_multiple_of, attribute, equals, item, longer_than, is_not_none, not_equal, \
has_attr
has_attr, p_all, p_any
class TestPredicates(unittest.TestCase):
def test_p_all(self):
p = [1, 2]
self.assertTrue(p_all(item(equals(1), 0), item(equals(2), 1))(p))
self.assertFalse(p_all(item(equals(1), 0), item(equals(3), 1))(p))
def test_p_any(self):
p = [1, 2]
self.assertTrue(p_any(item(equals(1), 0), item(equals(3), 1))(p))
self.assertFalse(p_any(item(equals(4), 0), item(equals(3), 1))(p))
def test_has_attr(self):
class A:
def __init__(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment