Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

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

Project Description

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


from pyconditions.pre import *

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

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

@Custom("a", lambda x: x % 2 == 0)
@Custom("b", lambda x: not x % 2 == 0)
def evenOdd(a, b):
    return a * b

The documenting is there with the code it self, and if you violate the preconditions then a PyCondition exception is thrown with a much nicer error message than broken code.

evenOdd(3, 1)
pyconditions.exceptions.PyCondition: 3 did not pass the custom condition for parameter 'a' in function evenOdd

How about some postconditions?

from import *

def test(a):
    return a

@Custom(lambda a: a % 2 == 0)
def even(a):
    return a
test( None )
pyconditions.exception.PyCondition: The return value for uber.awesome.project.test was None

You can also mix the two as well.

from pyconditions import pre
from pyconditions import post

@pre.Custom("a", lamda a: a % 2 == 0)
@post.Custom(lambda a: a % 2 == 0)
def superSafeEven(a):
    return a

Want some class invariant shenanigans?

from pyconditions.invariant import Invariant, FieldsNotNone

class Test:

    def __init__(self):
        self.test = 1

    def add(self):
        return self.test + 1

    def set(self, v):
        self.test = v

t = Test()
print t.add()

That last call to add will cause the invariant to fail and thus throw the following:

pyconditions.exceptions.PyCondition: Field "test" was None when it should not have been in invariant "notNone"

Need a custom invariant?

from pyconditions.invariant import CustomInvariant

def invariant(self):
    return self.test == 1

@CustomInvariant("test", invariant)
class Test(object):

    def __init__(self):
        self.test = 1

    def method1(self):

This is great but the conditions slow my code down a lot? No problem.

from pyconditions.stage import Stage

stage = Stage()

Just set that somewhere in your code and you’ll be fine. There is still some overhead, mainly there will be two function calls for each method, the wrapper and the original function. But, for stacked Preconditions and Invariants it will not execute into the other conditions and invraiants when prod is called. If you want to go back to Dev then call dev().

Have conditions you want added? Open a PR with code.

Have an issue? Open a PR with fixed code.

Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
pyConditions-0.5.2.macosx-10.9-intel.exe (74.7 kB) Copy SHA256 Checksum SHA256 any Windows Installer Jul 6, 2014
pyConditions-0.5.2.tar.gz (11.0 kB) Copy SHA256 Checksum SHA256 Source Jul 6, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting