Skip to main content

Guava Like Preconditions in Python.

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 Pre
pre = Pre()

@pre.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 Pre
pre = Pre()

@pre.notNone( "a" )
@pre.between( "a", "a", "n" )
@pre.notNone( "b" )
@pre.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 Pre
pre = Pre()

BASES = [ 2, 3, 4 ]

@pre.custom( a, lambda x: x in BASES )
@pre.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.0.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

pyConditions-0.0.1.macosx-10.9-intel.exe (67.7 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for pyConditions-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0265df7c3b3cf30dac2989a6fc1a0f75c7dbd1b2f89f8d3b6282eb5f25947000
MD5 8083f1f2f9b1e7f682fbbf68644c28be
BLAKE2b-256 10fa5fd89532da3d48cfc14c8cb4bb175618023a5c02417a81908b79875926ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyConditions-0.0.1.macosx-10.9-intel.exe
Algorithm Hash digest
SHA256 c591362443817da83d3e1ae52cc21eadf0d4968c49d1547e56a8c17f39db7299
MD5 407be58cb16b183844e2994b388ffee1
BLAKE2b-256 847c4fa6f691d168694bfd92c462a29d56dcaf520ea615c926c64ff6feb5c1ae

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