N
Nathan Rice
PyPi name: constraintslib (you'll be dissapointed if you get
constraints by accident)
Docs: http://packages.python.org/constraintslib/
Github: https://github.com/nathan-rice/Constraints
Constraints - Sleek contract-style validation tools
===================================================
Constraints provides flexible validation tools for a variety of circumstances.
All validation in constraints is done by type checking. Constraints provides
a special abstract base class (Constraints) which facilitates on the fly
construction of validation types. Constraints also provides a special class
(Symbol) which can be used to generate natural, easy to read
constraint expressions.
for example::
# constraints.util for this purpose...
Constraint instances also provide descriptors which will verify values at set
time. For example::
... x = Constraints(X > 2)
...Traceback (most recent call last):
...
AssertionError: Specified value (1) does not satisfy this constraint
Design by contract style preconditions, postconditions and invariants are also
supported, and can be used either as context managers or function decorators::
... do_stuff()
...
Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified pre-condition... x -= 4
...
Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified post-condition... def foo(x):
... return x
...Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified pre-condition... def foo(x):
... return x - 5
...Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified post-condition
Symbol objects are very flexible, and provide a nice
way to specify your constraints without resorting to a domain specific language.
Symbol objects are fairly simple; whenever an operation is performed on them,
they capture it and return a new Symbol object wrapping the operation so that
it can be performed with concrete input at a later time. There are exceptions
to this, for example isinstance, which uses the metaclass method, and the type
constructors (str, int, bool, etc) which throw an error if the correct type is
not returned.
constraints by accident)
Docs: http://packages.python.org/constraintslib/
Github: https://github.com/nathan-rice/Constraints
From the docs:
Constraints - Sleek contract-style validation tools
===================================================
Constraints provides flexible validation tools for a variety of circumstances.
All validation in constraints is done by type checking. Constraints provides
a special abstract base class (Constraints) which facilitates on the fly
construction of validation types. Constraints also provides a special class
(Symbol) which can be used to generate natural, easy to read
constraint expressions.
for example::
# My apologies for the lambda spam. I provide some functions infrom constraints.proxy import Symbol
from constraints.constraints import Constraints
X = Symbol()
SizeConstraint = Constraints(X * 2 + 1 >= 5)
ModuloConstraint = Constraints(X % 2 != 0, X != 3)
CharacterConstraint = Constraints(X[-1] == "h")
# constraints.util for this purpose...
Truecallable_expr = lambda x: all(lambda x: isinstance(x, SizeConstraint), x)
CollectionConstraint = Constraint(callable_expr)
isinstance(1, SizeConstraint) False
isinstance(2, SizeConstraint) True
isinstance(1, ModuloConstraint) True
isinstance("blah", CharacterConstraint) True
isinstance([2, 3, 4, 5], CollectionConstraint)
Constraint instances also provide descriptors which will verify values at set
time. For example::
... x = Constraints(X > 2)
...Traceback (most recent call last):
...
AssertionError: Specified value (1) does not satisfy this constraint
Design by contract style preconditions, postconditions and invariants are also
supported, and can be used either as context managers or function decorators::
... do_stuff()
...
Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified pre-condition... x -= 4
...
Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified post-condition... def foo(x):
... return x
...Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified pre-condition... def foo(x):
... return x - 5
...Traceback (most recent call last):
...
AssertionError: The value (1) did not meet the specified post-condition
Symbol objects are very flexible, and provide a nice
way to specify your constraints without resorting to a domain specific language.
Symbol objects are fairly simple; whenever an operation is performed on them,
they capture it and return a new Symbol object wrapping the operation so that
it can be performed with concrete input at a later time. There are exceptions
to this, for example isinstance, which uses the metaclass method, and the type
constructors (str, int, bool, etc) which throw an error if the correct type is
not returned.