Simple Lightweight library to add pre- and post- conditions as decorators
Project description
Single Assertion
This library provides a clean and declarative way to define preconditions and postconditions for functions using decorators, inspired by the principles of Design by Contract. PyPI
Instead of scattering assert statements throughout your code, you can express input and output constraints in a structured and reusable way.
Usage
Going from:
def divideByPositive(a, b):
assert a >= 0
assert b >= 0
if b <= 1:
result = 0
assert result <= a
assert result >= 0
return result
result = a/b
assert result <= a
assert result >= 0
return result
to:
from single_assertion import PreCondition, PostCondition
@PreCondition(lambda a: a >= 0, "a")
@PreCondition(lambda b: b >= 0, "b")
@PostCondition(lambda r: r >= 0)
@PostCondition(lambda r, a: r <= a, "a")
def divideByPositive(a, b):
if b <= 1:
result = 0
return result
result = a / b
return result
Use class variables by calling self
from single_assertion import PreCondition, PostCondition
class SpecialAdd:
def __init__(self, a):
self.a = a
@PreCondition(lambda self: self.a == 1, "self")
@PreCondition(lambda b: b == 2, "b")
def add(self, b):
return self.a + b
Install
pip install single-assertion
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file single_assertion-1.0.1.tar.gz.
File metadata
- Download URL: single_assertion-1.0.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1555a1953bde29b38fce03e37235cbc0dd3f7cab2bd6c2120c2bd8d51eb6ece2
|
|
| MD5 |
ae24a6d993f30d0180979efff95e93b6
|
|
| BLAKE2b-256 |
3dff7fe1fa0a5e98ab06b8b51b4916f383613a083b1ebf4d8e0b36f2964eb660
|
File details
Details for the file single_assertion-1.0.1-py3-none-any.whl.
File metadata
- Download URL: single_assertion-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfa21594acb57cd29839ac0a3d6781d7fd7cd481b7e929e6ef974e0962a89710
|
|
| MD5 |
9b3d9ba7ed7d474c2891be56987634bb
|
|
| BLAKE2b-256 |
545393d04f169c0a97d6ba2d260dc8065bf4fedf7d00491ad0b162f01da29924
|