Metadata-Version: 2.1
Name: pydantic-logic
Version: 1.0.2
Summary: Build complex rules, operate them with Pydantic, and execute them in Python
Home-page: https://github.com/barabum0/pydantic-logic
License: MIT
Keywords: python,pydantic,logic,condition,validation,json
Author: Roman Poltorabatko
Author-email: r.m.poltorabatko@gmail.com
Requires-Python: >=3.10,<4.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: pydantic (>=2.0)
Project-URL: Repository, https://github.com/barabum0/pydantic-logic
Description-Content-Type: text/markdown

<div align="center">
  <h1>pydantic-logic</h1>
  <p>
    <img alt="License: MIT" src="https://img.shields.io/github/license/barabum0/pydantic-logic">
    <img alt="GitHub stars" src="https://img.shields.io/github/stars/barabum0/pydantic-logic">
    <a href="https://pypi.org/project/pydantic-logic">
        <img alt="PyPI version" src="https://img.shields.io/pypi/v/pydantic-logic.svg?logo=pypi&logoColor=FFE873">
    </a>
    <a href="https://github.com/psf/black">
        <img alt="code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg">
    </a>
    <a href="https://github.com/PyCQA/isort">
        <img alt="formatted with: isort" src="https://img.shields.io/badge/formatted%20with-isort-blue.svg">
    </a>
    <a href="https://mypy-lang.org/">
        <img alt="mypy: checked" src="https://www.mypy-lang.org/static/mypy_badge.svg">
    </a>
  </p>
</div>
Build complex rules, operate them with Pydantic, and execute them in Python

## Installation
```shell
pip install pydantic-logic
```

## Examples

### True expression
```python
from pydantic_logic import Logic, LogicExpression, Operator

data = {
    "Phone number": "123123123",
    "some_value": True,
    "some_other_value": 1,
}

logic = Logic(
    expressions=[
        LogicExpression.b("Phone number", Operator.NE, "adasdasdasd"),
        LogicExpression.b("some_value", Operator.EQ, True),
        LogicExpression.b("some_other_value", Operator.GT, 0),
    ]
)
assert logic.evaluate(data)
```

### False expression
```python
from pydantic_logic import Logic, LogicExpression, Operator

data = {
    "Phone number": "123123123",
    "some_value": True,
    "some_other_value": 1,
}

logic = Logic(
    expressions=[
        LogicExpression.b("Phone number", Operator.EQ, "123123123"),
        LogicExpression.b("some_value", Operator.NE, False),
        LogicExpression.b("some_other_value", Operator.LT, 1),
    ]
)
assert not logic.evaluate(data)
```

### Empty expression with default result
Suitable if the expressions are set by the end users, not the code
```python
from pydantic_logic import Logic

data = {
    "Phone number": "123123123",
    "some_value": True,
    "some_other_value": 1,
}

logic = Logic(
    expressions=[]
)
assert logic.evaluate(data, default_if_empty=True)
```

### Get wrong expressions
```python
from pydantic_logic import Logic, LogicExpression, Operator

data = {
    "Phone number": "123123123",
    "some_value": True,
    "some_other_value": 1,
}

logic = Logic(
    expressions=[
        LogicExpression.b("Phone number", Operator.EQ, "123123123"),
        LogicExpression.b("some_value", Operator.NE, False),
        LogicExpression.b("some_other_value", Operator.LT, 1),
    ]
)
logic.get_wrong_expressions(data)  # second and third
```

