Skip to main content

Commenting sucks so let your code do it for you with Guava like preconditions that also actually do something.

Project description

Guava like precondition enforcing for Python.

Has been tested against:

  • 2.6

  • 2.7

  • 3.2

  • 3.3

  • pypy

Decorate functions with preconditions so that your code documents itself and at the same time removes the boilerplate code that is typically required when checking parameters.

An Example:

def divideAby1or10( a, b ):
    if not ( 1 <= b <= 10 ):
        <raise some error>
    else:
        return a / b

Simply becomes the following:

from pyconditions.pre import Between

@Between( "b", 1, 10 )
def divideAbyB( a, b )
  return a / b

In the above example the precondition pre.between ensures that the b variable is between (1, 10) inclusive. If it is not then a PyCondition exception is thrown with the error message detailing what went wrong.

More Examples:

from pyconditions.pre import Between, NotNone

@NotNone( "a" )
@Between( "a", "a", "n" )
@NotNone( "b" )
@Between( "b", "n", "z" )
def concat( a, b ):
  return a + b

The above ensures that the variables a and b are never None and that a is between ( ?a?, ?n? ) inclusively and b is between ( ?n?, ?z? ) inclusively.

from pyconditions.pre import Custom

BASES = [ 2, 3, 4 ]

@Custom( a, lambda x: x in BASES )
@Custom( b, lambda x: x % 2 == 0 )
def weirdMethod( a, b ):
    return a ** b

Using the custom precondition you are able to pass in any function that receives a single parameter and perform whatever condition checking you need.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyConditions-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

pyConditions-0.1.1.macosx-10.9-intel.exe (68.2 kB view details)

Uploaded Source

File details

Details for the file pyConditions-0.1.1.tar.gz.

File metadata

File hashes

Hashes for pyConditions-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a4e2cbb02d2ec1df86069026a5ab2cde386795828022d6b8ba87a7e128f04810
MD5 d73333440b7cc0c6b8a1c614d200c48c
BLAKE2b-256 3c09e9b0e844961c9259da7f110487df65e4b1bfe033181fac392fc19130121c

See more details on using hashes here.

File details

Details for the file pyConditions-0.1.1.macosx-10.9-intel.exe.

File metadata

File hashes

Hashes for pyConditions-0.1.1.macosx-10.9-intel.exe
Algorithm Hash digest
SHA256 4fa44fea86eaf4ad0c7b17fc131fcafa82e946af28fa95a121b3dc244252064c
MD5 cfeaeee9ac08a7e0263c7573035b87ee
BLAKE2b-256 402bc6ec95233ae36bcad41ca7f5d97dde081ac4a5f714520f67cd325f7c4754

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page