Skip to main content

Pysertive ✔️

Pysertive Logo

Latest release build status (GitHub Actions) code coverage PyPI status checked with mypy pre-commit enabled code style: black


Pysertive: Assertive Python Design by Contract (DbC) Toolkit

What is it?

Pysertive is a Python library that provides decorators for implementing Design by Contract (DbC) principles. It simplifies enforcing preconditions, postconditions, and invariants in your code. Pysertive aims to be a powerful tool for ensuring code behavior and constraints, promoting secure, maintainable, and robust software development in Python.

Table of Contents

🌟 Features

The things that Pysertive does well:

  • Preconditions: Ensure that function inputs meet defined criteria before execution.
  • Postconditions: Validate that the function outputs conform to expected conditions after execution.
  • Invariants: Guarantee that certain conditions remain true throughout the lifecycle of class instances.

Pysertive is designed with simplicity and flexibility in mind, allowing you to easily integrate rigorous contract checks into your Python code, which helps in debugging and maintaining complex systems.

📦 Where to get it?

To install Pysertive, simply use pip:

pip install pysertive

🚀 Quick Start

Here's how to quickly get started with Pysertive:

from pysertive import pre_condition, post_condition, invariant

@pre_condition(lambda x: x > 0, exception_type=ValueError, message="Input must be positive")
def sqrt(x):
    return x ** 0.5

@post_condition(lambda result: result != None, exception_type=AssertionError, message="Result cannot be None")
def fetch_data():
    return {"data": "Here is your data"}

@invariant(lambda self: self.balance >= 0, exception_type=RuntimeError, message="Insufficient funds")
class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        self.balance -= amount  # No need to manually check for negative balance

🔧 Usage

Using Preconditions

Ensure inputs to your functions are valid:

@pre_condition(lambda age: age >= 18, exception_type=ValueError, message="Must be 18 or older")
def sign_contract(age):
    print(f"Contract signed by individual aged {age}")

Using Postconditions

Validate outputs after your functions execute:

@post_condition(lambda result: result > 0, exception_type=AssertionError, message="Profit must be positive")
def calculate_profit(revenue, costs):
    return revenue - costs

Using Invariants

Enforce class states remain consistent:

@invariant(lambda self: self.inventory_count >= 0, exception_type=RuntimeError, message="Inventory count cannot be negative")
class Warehouse:
    def __init__(self, inventory_count):
        self.inventory_count = inventory_count

    def add_stock(self, number):
        self.inventory_count += number

    def remove_stock(self, number):
        self.inventory_count -= number

❓ Why Pysertive?

  • Reliability: Enforce rules consistently across your application.
  • Maintainability: Easier to manage and update code with clear contractual obligations.
  • Security: Prevents unexpected behaviors by strictly checking function inputs and outputs.

📚 Examples

For more detailed examples of how to use Pysertive, check out the examples.py file in the repository. This file contains examples of how to use preconditions, postconditions, and invariants in your Python code.

🤝 Contributing

Contributions are welcome! If you'd like to contribute, please check out the Contributing Guide, and feel free to open an issue or a pull request.

📜 License

Pysertive is released under the MIT License. See the License file for more details.


Go to Top

Metadata

Release files for pysertive 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysertive 0.0.1
File Size Uploaded Provenance Actions
pysertive-0.0.1.tar.gz 4.5 kB Without provenance Details for pysertive-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pysertive 0.0.1
File Interpreter ABI Platform Actions
pysertive-0.0.1-py3-none-any.whl Python 3 none any Details for pysertive-0.0.1-py3-none-any.whl

Total release size: 9.4 kB

Release files / pysertive-0.0.1.tar.gz

Download URL pysertive-0.0.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d979640457722ca5ed54af0b85dbce9e8eb81c0549e4a31f811e8195f91803a9
BLAKE2b-256 checksum
How to use checksums
350054132e5f6e5a4e82b648d2cfaec2c333a94b092f3f6e695d6c0dcb1f0876
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1021-azure

Release files / pysertive-0.0.1-py3-none-any.whl

Download URL pysertive-0.0.1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdb7b63677c0f017a2cb17c9ecdf1db88923d2807c6d0dd37d01802f02055003
BLAKE2b-256 checksum
How to use checksums
2a0c835aa9be91583a3055ce0650157cd9d482ba32a9d4fa0500eb3603d3cca0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1021-azure

Security for pysertive 0.0.1

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.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page