constraints - Validation utility classes

constraints provides several facilities:

Constraint classes generated by Constraints can validate instance attribute values when used as descriptors. The condition classes provide context managers, and both Precondition and Postcondition can be used as function decorators.

class constraints.constraints.ConditionBase(constraint, target=None, name=None)[source]

Base class for design by contract style conditions.

constraints.constraints.ConstraintBase[source]

Constraint base class. Constraints are usable as descriptors.

class constraints.constraints.Constraints(*args)[source]

Metaclass which provides constraint verification for objects. Constraints are specified as arguments to the __new__ method. Constraints can be either no argument callables or Symbol expressions.

isinstance(obj, ConstraintsInstance) will return True iff obj satisfies all constraints.

class constraints.constraints.Invariant(constraint, target=None, name=None)[source]

Constraint container that verifies an invariant condition. Usable as a context manager.

class constraints.constraints.Postcondition(constraint, target=None, name=None)[source]

Constraint container that verifies a postcondition. Usable as a decorator or context manager.

Note

The decorator constrains the return value of the decorated function. It ignores the callable and name attributes of the condition if they are present.

decorator(f, *args, **kwargs)[source]

Postcondition decorator, looks at the result of the function call and verify that it is satisfies the constraint condition.

class constraints.constraints.Precondition(constraint, target=None, name=None)[source]

Constraint container that verifies a precondition. Usable as a decorator or context manager.

Note

You must specify a string argument name for the precondition or the decorator will not function properly.

decorator(f, *args, **kwargs)[source]

Precondition decorator, looks at the bound value of the arg with the key equal to self.name.

Indices and tables

Table Of Contents

Previous topic

Constraints - Sleek contract-style validation tools

Next topic

proxy - Symbol proxy object for constraint generation

This Page