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

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

from pyconditions.stage import Stage

stage = Stage()
stage.prod()

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.

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

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyConditions-0.5.0.macosx-10.9-intel.tar.gz (14.8 kB view details)

Uploaded Source

pyConditions-0.5.0.macosx-10.9-intel.exe (74.7 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pyConditions-0.5.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyConditions-0.5.0.tar.gz
Algorithm Hash digest
SHA256 84c1977ba6f2fd126e56f199e2ed7e935bea67ea91b4cba4cf980f9640202397
MD5 ab79136e8ad93e59f7a9d222bbfa62e6
BLAKE2b-256 f64330919111b6cf366bf7b450d031ada516c6bcf8e980e4f174733434e117c8

See more details on using hashes here.

File details

Details for the file pyConditions-0.5.0.macosx-10.9-intel.tar.gz.

File metadata

File hashes

Hashes for pyConditions-0.5.0.macosx-10.9-intel.tar.gz
Algorithm Hash digest
SHA256 a211ffa58b1a0411685f4f7eb35d0bffe7577f997d0b8b70d2927274a9c0ddbc
MD5 82b47a90f196fab6a2927015852f7a91
BLAKE2b-256 be999bad0cfe87bdd1bda592a2a1ef56ed93bfc7cef6927701788ce28445ce01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyConditions-0.5.0.macosx-10.9-intel.exe
Algorithm Hash digest
SHA256 966e6bc4a891f40b55b2fce9b7b62f3283dba015ae479c2054233ebfc541f130
MD5 923633ebb1251609337dccfc9e356493
BLAKE2b-256 8e37943d04ce3267a8ff799c2b750f74ec2bfd49b1d316db2cbe126e1d40a513

See more details on using hashes here.

Supported by

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