pyConditions
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.
Metadata
Release files for pyConditions 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyConditions-0.5.2.tar.gz | 11.0 kB | Without provenance | Details for pyConditions-0.5.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyConditions-0.5.2.macosx-10.9-intel.exe | Details for pyConditions-0.5.2.macosx-10.9-intel.exe |
Total release size: 85.7 kB
Release files / pyConditions-0.5.2.tar.gz
| Download URL | pyConditions-0.5.2.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a58f9617fb60c5272d0f445058487b7fce665af6791183c541a328eeeeff3214
|
|
BLAKE2b-256 checksum How to use checksums |
5d7929652f4e2f9b04af0e863bfb671bd2e618967e3b6bd44fde98f2bc260a8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pyConditions-0.5.2.macosx-10.9-intel.exe
| Download URL | pyConditions-0.5.2.macosx-10.9-intel.exe |
|---|---|
| Size | 74.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3fc3fba9f07749e3ccea5d2f9d80be6877bc1b8f253a786d8fd08cbd405c0fcc
|
|
BLAKE2b-256 checksum How to use checksums |
f7d078c58c44599b49bc5555c2a18bdc5e8de1ba0bdee79a59f249c2ef0cc99f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pyConditions 0.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.