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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyConditions-0.0.2.tar.gz
.
File metadata
- Download URL: pyConditions-0.0.2.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e688cce08f164265a09c8fa925afdba84f5b10390bf0065a01b229fcbbe995f2 |
|
MD5 | bae59a137cff660cee4bc0333073724d |
|
BLAKE2b-256 | b2b0df8fc124925724e5805bc9d9a817d11e8b79e115f2ca46edc949a936275d |
File details
Details for the file pyConditions-0.0.2.macosx-10.9-intel.exe
.
File metadata
- Download URL: pyConditions-0.0.2.macosx-10.9-intel.exe
- Upload date:
- Size: 67.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa97ccfc8c0fa6df6afb8088a767e9901529af52eaceaf231660ac10a4b6f996 |
|
MD5 | 53d93a471ef4f01e58fa581866e950db |
|
BLAKE2b-256 | 463f1ab9e3e9df696f1f72b1ec4dd6e0bdf0bfd68c9d4adf87e4b0555107d5cd |