Build complex rules, operate them with Pydantic, and execute them in Python
Project description
Build complex rules, operate them with Pydantic, and execute them in Python
Installation
pip install pydantic-logic
Examples
True expression
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
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
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
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydantic_logic-1.0.3.tar.gz
(3.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_logic-1.0.3.tar.gz.
File metadata
- Download URL: pydantic_logic-1.0.3.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d6238f5fa0dd8e6a14985324acc06cb0e1cc513cca462731c4d981acfe29445
|
|
| MD5 |
e0270cfdbfb082e89350b4bb36fc64a1
|
|
| BLAKE2b-256 |
04d90cf570405e45573852ccdded25a9ff0be39c388208d0d1942079338ad6e3
|
File details
Details for the file pydantic_logic-1.0.3-py3-none-any.whl.
File metadata
- Download URL: pydantic_logic-1.0.3-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17ea12645fc4235057a00345ffa5c69f86e18945b2094f891661cea9943a9a33
|
|
| MD5 |
673338728e3fb3c95de142c3c40b6453
|
|
| BLAKE2b-256 |
b9e9de52d3c3197bd88c732893ef03ebd937b24541cea40a294e187fc0fa99ea
|