Pysertive ✔️
=========
<p align="center">
<img src="assets/pysertive.png" alt="Pysertive Logo" width="200" height="200">
</p>
<p align="center">
<a href="https://github.com/abeltavares/pysertive/releases">
<img src="https://badgen.net/github/release/abeltavares/pysertive" alt="Latest release">
</a>
<a href="https://github.com/abeltavares/pysertive/actions/workflows/unit-tests.yml">
<img src="https://github.com/abeltavares/pysertive/actions/workflows/unit-tests.yml/badge.svg" alt="build status (GitHub Actions)">
</a>
<!-- COVERAGE_BADGE_URL -->
<a href="https://img.shields.io/badge/coverage-100%25-green">
<img src="https://img.shields.io/badge/coverage-100%25-green" alt="code coverage">
</a>
<!-- END_COVERAGE_BADGE_URL -->
<a href="https://pypi.python.org/pypi/pysertive">
<img src="https://img.shields.io/pypi/v/reader.svg" alt="PyPI status">
</a>
<a href="http://mypy-lang.org/">
<img src="http://www.mypy-lang.org/static/mypy_badge.svg" alt="checked with mypy">
</a>
<a href="https://github.com/pre-commit/pre-commit">
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white" alt="pre-commit enabled">
</a>
<a href="https://github.com/psf/black">
<img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="code style: black">
</a>
</p>
<br>
<div align="center">
**Pysertive**: Assertive Python Design by Contract (DbC) Toolkit
</div>
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](#features)
- [Where to get it?](#where-to-get-it)
- [Quick Start](#quick-start)
- [Usage](#usage)
- [Why Pysertive?](#why-pysertive)
- [Examples](#examples)
- [Contributing](#contributing)
- [License](#license)
🌟 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:
```bash
pip install pysertive
```
🚀 Quick Start
-----------
Here's how to quickly get started with Pysertive:
```python
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:
```python
@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:
```python
@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:
```python
@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](CONTRIBUTING.md), and feel free to open an issue or a pull request.
📜 License
-------
Pysertive is released under the MIT License. See the [License](LICENSE.txt) file for more details.
<hr>
[Go to Top](#table-of-contents)
Raw data
{
"_id": null,
"home_page": "https://github.com/abeltavares/pysertive",
"name": "pysertive",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "python, contract-programming, design-by-contract, preconditions, postconditions, invariants, code-quality, code-verification, software-reliability",
"author": "Abel Tavares",
"author_email": "abelst9@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/35/00/54132e5f6e5a4e82b648d2cfaec2c333a94b092f3f6e695d6c0dcb1f0876/pysertive-0.0.1.tar.gz",
"platform": null,
"description": "Pysertive \u2714\ufe0f\n=========\n\n<p align=\"center\">\n <img src=\"assets/pysertive.png\" alt=\"Pysertive Logo\" width=\"200\" height=\"200\">\n</p>\n\n<p align=\"center\">\n <a href=\"https://github.com/abeltavares/pysertive/releases\">\n <img src=\"https://badgen.net/github/release/abeltavares/pysertive\" alt=\"Latest release\">\n </a>\n <a href=\"https://github.com/abeltavares/pysertive/actions/workflows/unit-tests.yml\">\n <img src=\"https://github.com/abeltavares/pysertive/actions/workflows/unit-tests.yml/badge.svg\" alt=\"build status (GitHub Actions)\">\n </a>\n <!-- COVERAGE_BADGE_URL -->\n <a href=\"https://img.shields.io/badge/coverage-100%25-green\">\n <img src=\"https://img.shields.io/badge/coverage-100%25-green\" alt=\"code coverage\">\n </a>\n <!-- END_COVERAGE_BADGE_URL -->\n <a href=\"https://pypi.python.org/pypi/pysertive\">\n <img src=\"https://img.shields.io/pypi/v/reader.svg\" alt=\"PyPI status\">\n </a>\n <a href=\"http://mypy-lang.org/\">\n <img src=\"http://www.mypy-lang.org/static/mypy_badge.svg\" alt=\"checked with mypy\">\n </a>\n <a href=\"https://github.com/pre-commit/pre-commit\">\n <img src=\"https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white\" alt=\"pre-commit enabled\">\n </a>\n <a href=\"https://github.com/psf/black\">\n <img src=\"https://img.shields.io/badge/code%20style-black-000000.svg\" alt=\"code style: black\">\n </a>\n</p>\n\n<br>\n\n<div align=\"center\">\n\n**Pysertive**: Assertive Python Design by Contract (DbC) Toolkit\n\n</div>\n\nWhat is it?\n--------\n\nPysertive is a Python library that provides decorators for implementing Design by Contract (DbC) principles. It simplifies enforcing preconditions, postconditions, and invariants in your code. \nPysertive aims to be a powerful tool for ensuring code behavior and constraints, promoting secure, maintainable, and robust software development in Python.\n## Table of Contents\n\n- [Features](#features)\n- [Where to get it?](#where-to-get-it)\n- [Quick Start](#quick-start)\n- [Usage](#usage)\n- [Why Pysertive?](#why-pysertive)\n- [Examples](#examples)\n- [Contributing](#contributing)\n- [License](#license)\n\n\ud83c\udf1f Features\n--------\nThe things that Pysertive does well:\n\n- Preconditions: Ensure that function inputs meet defined criteria before execution.\n- Postconditions: Validate that the function outputs conform to expected conditions after execution.\n- Invariants: Guarantee that certain conditions remain true throughout the lifecycle of class instances.\n\nPysertive 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.\n\n\ud83d\udce6 Where to get it?\n------------\n\nTo install Pysertive, simply use pip:\n\n```bash\npip install pysertive\n```\n\n\ud83d\ude80 Quick Start\n-----------\n\nHere's how to quickly get started with Pysertive:\n\n```python\nfrom pysertive import pre_condition, post_condition, invariant\n\n@pre_condition(lambda x: x > 0, exception_type=ValueError, message=\"Input must be positive\")\ndef sqrt(x):\n return x ** 0.5\n\n@post_condition(lambda result: result != None, exception_type=AssertionError, message=\"Result cannot be None\")\ndef fetch_data():\n return {\"data\": \"Here is your data\"}\n\n@invariant(lambda self: self.balance >= 0, exception_type=RuntimeError, message=\"Insufficient funds\")\nclass BankAccount:\n def __init__(self, balance):\n self.balance = balance\n\n def deposit(self, amount):\n self.balance += amount\n\n def withdraw(self, amount):\n self.balance -= amount # No need to manually check for negative balance\n```\n\n\ud83d\udd27 Usage\n-----\n\n### Using Preconditions\n\nEnsure inputs to your functions are valid:\n\n```python\n@pre_condition(lambda age: age >= 18, exception_type=ValueError, message=\"Must be 18 or older\")\ndef sign_contract(age):\n print(f\"Contract signed by individual aged {age}\")\n```\n\n### Using Postconditions\n\nValidate outputs after your functions execute:\n\n```python\n@post_condition(lambda result: result > 0, exception_type=AssertionError, message=\"Profit must be positive\")\ndef calculate_profit(revenue, costs):\n return revenue - costs\n```\n\n### Using Invariants\n\nEnforce class states remain consistent:\n\n```python\n@invariant(lambda self: self.inventory_count >= 0, exception_type=RuntimeError, message=\"Inventory count cannot be negative\")\nclass Warehouse:\n def __init__(self, inventory_count):\n self.inventory_count = inventory_count\n\n def add_stock(self, number):\n self.inventory_count += number\n\n def remove_stock(self, number):\n self.inventory_count -= number\n```\n\n\u2753 Why Pysertive?\n--------------\n\n- Reliability: Enforce rules consistently across your application.\n- Maintainability: Easier to manage and update code with clear contractual obligations.\n- Security: Prevents unexpected behaviors by strictly checking function inputs and outputs.\n\n## \ud83d\udcda Examples\n\nFor 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.\n\n\ud83e\udd1d Contributing\n------------\n\nContributions are welcome! If you'd like to contribute, please check out the [Contributing Guide](CONTRIBUTING.md), and feel free to open an issue or a pull request.\n\n\ud83d\udcdc License\n-------\n\nPysertive is released under the MIT License. See the [License](LICENSE.txt) file for more details.\n\n<hr>\n\n[Go to Top](#table-of-contents)",
"bugtrack_url": null,
"license": null,
"summary": null,
"version": "0.0.1",
"project_urls": {
"Homepage": "https://github.com/abeltavares/pysertive",
"Repository": "https://github.com/abeltavares/pysertive.git"
},
"split_keywords": [
"python",
" contract-programming",
" design-by-contract",
" preconditions",
" postconditions",
" invariants",
" code-quality",
" code-verification",
" software-reliability"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2a0c835aa9be91583a3055ce0650157cd9d482ba32a9d4fa0500eb3603d3cca0",
"md5": "095376812c7e79f2baf21ad43f4318aa",
"sha256": "fdb7b63677c0f017a2cb17c9ecdf1db88923d2807c6d0dd37d01802f02055003"
},
"downloads": -1,
"filename": "pysertive-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "095376812c7e79f2baf21ad43f4318aa",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 4975,
"upload_time": "2024-05-21T19:35:40",
"upload_time_iso_8601": "2024-05-21T19:35:40.296578Z",
"url": "https://files.pythonhosted.org/packages/2a/0c/835aa9be91583a3055ce0650157cd9d482ba32a9d4fa0500eb3603d3cca0/pysertive-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "350054132e5f6e5a4e82b648d2cfaec2c333a94b092f3f6e695d6c0dcb1f0876",
"md5": "09f36c4dc7bb97848a76bd343cf280da",
"sha256": "d979640457722ca5ed54af0b85dbce9e8eb81c0549e4a31f811e8195f91803a9"
},
"downloads": -1,
"filename": "pysertive-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "09f36c4dc7bb97848a76bd343cf280da",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 4475,
"upload_time": "2024-05-21T19:35:41",
"upload_time_iso_8601": "2024-05-21T19:35:41.651277Z",
"url": "https://files.pythonhosted.org/packages/35/00/54132e5f6e5a4e82b648d2cfaec2c333a94b092f3f6e695d6c0dcb1f0876/pysertive-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-21 19:35:41",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "abeltavares",
"github_project": "pysertive",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pysertive"
}