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