Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19499

Constraints -//- first release -//- Flexible abstract class based validation for attributes, functions and code blocks

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nathan.alexander.rice@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'pypi': 0.04; 'method,': 0.07; 'returned.': 0.07; 'subject:based': 0.07; 'subject:code': 0.07; 'descriptors': 0.09; 'exceptions': 0.09; 'expressions.': 0.09; 'facilitates': 0.09; 'supported,': 0.09; 'throw': 0.09; 'url:github': 0.09; 'url:packages': 0.09; 'def': 0.13; '(str,': 0.16; 'foo(object):': 0.16; 'lambda': 0.16; 'metaclass': 0.16; 'subject: \n ': 0.16; 'subject:release': 0.16; '>>>': 0.18; '(most': 0.21; 'later': 0.21; 'to:2**1': 0.21; 'input': 0.21; 'int,': 0.23; 'specify': 0.24; 'traceback': 0.24; 'function': 0.27; 'import': 0.27; 'language.': 0.28; 'message- id:@mail.gmail.com': 0.28; 'example': 0.28; 'class': 0.29; 'fairly': 0.30; 'checking.': 0.30; 'received:209.85.210.46': 0.30; 'received:mail-pz0-f46.google.com': 0.30; 'types.': 0.30; 'error': 0.30; 'specified': 0.30; 'correct': 0.31; 'values': 0.32; 'capture': 0.32; 'does': 0.32; 'objects': 0.32; 'done': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'there': 0.33; 'last):': 0.34; 'symbol': 0.34; '...': 0.35; 'url:python': 0.36; 'uses': 0.36; 'to:addr:python-announce-list': 0.36; 'to:name :python-list': 0.36; 'received:google.com': 0.37; 'some': 0.38; 'received:209.85': 0.38; 'url:org': 0.39; 'either': 0.39; 'received:209': 0.39; 'did': 0.39; 'to:addr:python.org': 0.40; '(1)': 0.40; 'type': 0.60; 'design': 0.61; 'meet': 0.61; 'your': 0.61; 'managers': 0.62; 'construction': 0.64; 'sfxlen:3': 0.66; 'skip:= 50': 0.66; 'special': 0.67; 'validation': 0.73; 'constraint': 0.84; 'flexible,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=UAmW2vT3kibrTZqWhK2YBIedM+bIl8KmVstrHf+eEtU=; b=fHZhU80RTmrpUF+w2P/eCDDv9rkyqGrE2qSkJQLKn0HXZA/YVwmnJR4MhwoRwFXa9E t4uhDYAhKWJKOxPAr6VqHKEsJql0iy3sOEH47msiW4ly3Yyul/yYZzEVrbFkeAapP/KG EFdCzC81k2HP/h5F/+n0MMhuxtnYRujyDZkR0=
MIME-Version 1.0
Date Thu, 26 Jan 2012 14:36:29 -0500
Subject Constraints -//- first release -//- Flexible abstract class based validation for attributes, functions and code blocks
From Nathan Rice <nathan.alexander.rice@gmail.com>
To python-list <python-list@python.org>, python-announce-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5134.1327606592.27778.python-list@python.org> (permalink)
Lines 96
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327606593 news.xs4all.nl 6858 [2001:888:2000:d::a6]:38433
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19499

Show key headers only | View raw


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

>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::

   >>> from 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")
   # My apologies for the lambda spam.  I provide some functions in
   # constraints.util for this purpose...
   >>> callable_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)
   True

Constraint instances also provide descriptors which will verify values at set
time.  For example::

   >>> class Foo(object):
   ...    x = Constraints(X > 2)
   ...
   >>> bar = Foo()
   >>> bar.x = 1
   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::

   >>> x_pre = SizeConstraint.precondition("x")
   >>> x_post = SizeConstraint.postcondition("x")
   >>> x = 1
   >>> with x_pre:
   ...   do_stuff()
   ...
   Traceback (most recent call last):
      ...
   AssertionError: The value (1) did not meet the specified pre-condition
   >>> x = 5
   >>> with x_post:
   ...   x -= 4
   ...
   Traceback (most recent call last):
      ...
   AssertionError: The value (1) did not meet the specified post-condition
   >>> @x_pre
   ... def foo(x):
   ...    return x
   ...
   >>> foo(1)
   Traceback (most recent call last):
      ...
   AssertionError: The value (1) did not meet the specified pre-condition
   >>> @x_post
   ... def foo(x):
   ...    return x - 5
   ...
   >>> foo(6)
   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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Constraints -//- first release -//- Flexible abstract class based validation for attributes, functions and code blocks Nathan Rice <nathan.alexander.rice@gmail.com> - 2012-01-26 14:36 -0500

csiph-web