pycsi


Namepycsi JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/EM51641/csi
SummarySimple Validation Library for Python
upload_time2023-09-14 13:37:53
maintainer
docs_urlNone
authorElyes
requires_python>=3.10.12,<4.0.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            | | |
| --- | --- |
| Python| ![Python](https://img.shields.io/pypi/pyversions/pycsi) |
| Package | [![PyPI Latest Release](https://img.shields.io/pypi/v/pycsi.svg)](https://pypi.org/project/pycsi/) [![PyPI Downloads](https://img.shields.io/pypi/dm/pycsi.svg?label=PyPI%20downloads)](https://pypi.org/project/pycsi/) [![codecov](https://codecov.io/gh/EM51641/csi/graph/badge.svg?token=K1VoKFacbb)](https://codecov.io/gh/EM51641/csi)|
| Meta | [![License - MIT](https://img.shields.io/pypi/l/pycsi.svg)](https://github.com/EM51641/csi/blob/main/LICENSE)|


# pycsi

pycsi is a simple python library to validate data and/or build a validation schema.

# Installation

```bash
pip install pycsi
```

# Usage
We might begin by creating validation callables such:
    
```python
def is_divisible_by_three(x: float):
    return x % 3 == 0

def is_all_positive(numbers: list[float]):
    return all(number > 0 for number in numbers)

def is_quotient_positive(x: float, y: float):
    return x / y > 0

```

Then we can instantiate a ```Subvalidator``` instance and  the validation callables to it:

```python
from pycsi import Subvalidator

first_subvalidator = Subvalidator(
    "First condition",
    "Number is not divisible by 3",
    is_divisible_by_three,
    x=3
    )

second_subvalidator = Subvalidator(
    "Second condition",
    "Not every number is positive",
    is_all_positive,
    numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, -10],
    )

third_subvalidator = Subvalidator(
    "Third condition",
    "Quotient is not positive",
    is_quotient_positive,
    x=10,
    y=2,
    )
```

Finally we can create a validator object and add the subvalidators to it.

```python
from pycsi import Validator

validator = Validator()

validator.add(first_subvalidator)
validator.add(second_subvalidator)
validator.add(third_subvalidator)

validator.run()
```

The validator will run all the subvalidators and the `unvalidated_set` property will return a set of the failed subvalidators.
```python
print(validator.unvalidated_set)
{ValidatorError(condition='Second condition', error_message='Not every number is positive')}
```

# Asynchronous Usage

This library also supports async validation. We can create asyncronous validation callables such:

```python
import asyncio

async def async_is_divisible_by_three(x: float) -> bool:
    await asyncio.sleep(0.1)
    return is_divisible_by_three(x)

async def async_is_all_positive(numbers: list[float]) -> bool:
    await asyncio.sleep(0.1)
    return is_all_positive(numbers)
```

Instead of using the ```Subvalidator``` API, we are going to use the ```AsyncSubvalidator``` in the next step:

```python
from pycsi import AsyncSubvalidator

first_subvalidator = AsyncSubvalidator(
    "First condition",
    "Number is not divisible by 3",
    async_is_divisible_by_three,
    x=10,
    )

second_subvalidator = AsyncSubvalidator(
    "Second condition",
    "Not every number is positive",
    async_is_all_positive,
    numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    )
```

Same for the ```Validator``` API, we will switch the the ```AsyncValidator```.

```python
from pycsi import AsyncValidator

validator = AsyncValidator()
validator.add(first_subvalidator)
validator.add(second_subvalidator)

validator.run()
print(validator.unvalidated_set)
{ValidatorError(condition='First condition', error_message='Number is not divisible by 3')}
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/EM51641/csi",
    "name": "pycsi",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10.12,<4.0.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Elyes",
    "author_email": "elyesmahjoubi@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/59/3d/a142c8dd3e95af07e4675b47ed75e3900a6d4d2229a5bebf1035d656d3b0/pycsi-0.1.0.tar.gz",
    "platform": null,
    "description": "| | |\n| --- | --- |\n| Python| ![Python](https://img.shields.io/pypi/pyversions/pycsi) |\n| Package | [![PyPI Latest Release](https://img.shields.io/pypi/v/pycsi.svg)](https://pypi.org/project/pycsi/) [![PyPI Downloads](https://img.shields.io/pypi/dm/pycsi.svg?label=PyPI%20downloads)](https://pypi.org/project/pycsi/) [![codecov](https://codecov.io/gh/EM51641/csi/graph/badge.svg?token=K1VoKFacbb)](https://codecov.io/gh/EM51641/csi)|\n| Meta | [![License - MIT](https://img.shields.io/pypi/l/pycsi.svg)](https://github.com/EM51641/csi/blob/main/LICENSE)|\n\n\n# pycsi\n\npycsi is a simple python library to validate data and/or build a validation schema.\n\n# Installation\n\n```bash\npip install pycsi\n```\n\n# Usage\nWe might begin by creating validation callables such:\n    \n```python\ndef is_divisible_by_three(x: float):\n    return x % 3 == 0\n\ndef is_all_positive(numbers: list[float]):\n    return all(number > 0 for number in numbers)\n\ndef is_quotient_positive(x: float, y: float):\n    return x / y > 0\n\n```\n\nThen we can instantiate a ```Subvalidator``` instance and  the validation callables to it:\n\n```python\nfrom pycsi import Subvalidator\n\nfirst_subvalidator = Subvalidator(\n    \"First condition\",\n    \"Number is not divisible by 3\",\n    is_divisible_by_three,\n    x=3\n    )\n\nsecond_subvalidator = Subvalidator(\n    \"Second condition\",\n    \"Not every number is positive\",\n    is_all_positive,\n    numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, -10],\n    )\n\nthird_subvalidator = Subvalidator(\n    \"Third condition\",\n    \"Quotient is not positive\",\n    is_quotient_positive,\n    x=10,\n    y=2,\n    )\n```\n\nFinally we can create a validator object and add the subvalidators to it.\n\n```python\nfrom pycsi import Validator\n\nvalidator = Validator()\n\nvalidator.add(first_subvalidator)\nvalidator.add(second_subvalidator)\nvalidator.add(third_subvalidator)\n\nvalidator.run()\n```\n\nThe validator will run all the subvalidators and the `unvalidated_set` property will return a set of the failed subvalidators.\n```python\nprint(validator.unvalidated_set)\n{ValidatorError(condition='Second condition', error_message='Not every number is positive')}\n```\n\n# Asynchronous Usage\n\nThis library also supports async validation. We can create asyncronous validation callables such:\n\n```python\nimport asyncio\n\nasync def async_is_divisible_by_three(x: float) -> bool:\n    await asyncio.sleep(0.1)\n    return is_divisible_by_three(x)\n\nasync def async_is_all_positive(numbers: list[float]) -> bool:\n    await asyncio.sleep(0.1)\n    return is_all_positive(numbers)\n```\n\nInstead of using the ```Subvalidator``` API, we are going to use the ```AsyncSubvalidator``` in the next step:\n\n```python\nfrom pycsi import AsyncSubvalidator\n\nfirst_subvalidator = AsyncSubvalidator(\n    \"First condition\",\n    \"Number is not divisible by 3\",\n    async_is_divisible_by_three,\n    x=10,\n    )\n\nsecond_subvalidator = AsyncSubvalidator(\n    \"Second condition\",\n    \"Not every number is positive\",\n    async_is_all_positive,\n    numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n    )\n```\n\nSame for the ```Validator``` API, we will switch the the ```AsyncValidator```.\n\n```python\nfrom pycsi import AsyncValidator\n\nvalidator = AsyncValidator()\nvalidator.add(first_subvalidator)\nvalidator.add(second_subvalidator)\n\nvalidator.run()\nprint(validator.unvalidated_set)\n{ValidatorError(condition='First condition', error_message='Number is not divisible by 3')}\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple Validation Library for Python",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/EM51641/csi",
        "Repository": "https://github.com/EM51641/csi"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8fa21e18d3ff770f3aa8f39a4da608fe6aa1feb4d2333ba264f96ed1b3f398d1",
                "md5": "8a7d4e2b5f780385057fbebbc05daac3",
                "sha256": "77538b9ec88d2d12f0373695d9ad588d621e46e30662d14d77ecb7390cc023d2"
            },
            "downloads": -1,
            "filename": "pycsi-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8a7d4e2b5f780385057fbebbc05daac3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10.12,<4.0.0",
            "size": 7052,
            "upload_time": "2023-09-14T13:37:51",
            "upload_time_iso_8601": "2023-09-14T13:37:51.327478Z",
            "url": "https://files.pythonhosted.org/packages/8f/a2/1e18d3ff770f3aa8f39a4da608fe6aa1feb4d2333ba264f96ed1b3f398d1/pycsi-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "593da142c8dd3e95af07e4675b47ed75e3900a6d4d2229a5bebf1035d656d3b0",
                "md5": "9a268143b264e7f22064058bc7edde33",
                "sha256": "7b7e2d7e0bc2c2f12240a28a5d72fc41c2f26ccedadf321a4b0f71f820808857"
            },
            "downloads": -1,
            "filename": "pycsi-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9a268143b264e7f22064058bc7edde33",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10.12,<4.0.0",
            "size": 5052,
            "upload_time": "2023-09-14T13:37:53",
            "upload_time_iso_8601": "2023-09-14T13:37:53.125365Z",
            "url": "https://files.pythonhosted.org/packages/59/3d/a142c8dd3e95af07e4675b47ed75e3900a6d4d2229a5bebf1035d656d3b0/pycsi-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-14 13:37:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "EM51641",
    "github_project": "csi",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pycsi"
}
        
Elapsed time: 0.54628s