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

Build Status

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

Examples:

from pyconditions.pre import *

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

@NotNone( "a" )
@Between( "a", "a", "n" )
@NotNone( "b" )
@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 pyconditions.post import *

@NotNone()
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
@FieldsNotNone( [ "test" ] )
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()
t.set( None )

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 ):
           self.test = 2

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

Have an issue? Open a PR with fixed code.

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.3.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

pyConditions-0.3.2.macosx-10.9-intel.exe (72.2 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for pyConditions-0.3.2.tar.gz
Algorithm Hash digest
SHA256 2df153f0fb3548ed01a3deb3721d3e517d04e62551439134354f35c463f9488b
MD5 36f960df9514b42d38b61bc9d1bae08b
BLAKE2b-256 785f8c793dff4ce503c774be5c3c491226eb6b6d066c693c52b2a8e896fd7ccd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyConditions-0.3.2.macosx-10.9-intel.exe
Algorithm Hash digest
SHA256 86df0d1a67da8e9274aa6215d3205bf4fc83af577d799209b3574a09843b940b
MD5 250e73a8a993fc9eeda3777a7dc064a4
BLAKE2b-256 d17906b32ed98ac5e01c73e2853cc33a449de7e7510109df101d51f906542fa6

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