Metadata-Version: 2.4
Name: single_assertion
Version: 1.0.0
Summary: Simple Lightweight library to add pre- and post- conditions as decorators
Project-URL: Homepage, https://github.com/klanting/SingleAssertion
Author: klanting
License-File: LICENSE
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# 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.

Instead of scattering `assert` statements throughout your code, you can express input and output constraints in a structured and reusable way.

Going from:
```python

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:
```python
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 why calling self

```python
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

```