Quickstart
Installation
Install ransacklib
from PyPI:
pip install ransacklib
Basic example
Here’s how to evaluate a simple query against structured data using ransack
:
from ransack import Filter, Parser
# Define the input data to evaluate
data = {
"a": {"nested_key": "ransack"},
"b": [1, 2, 3],
"c": 1,
}
# Define the evaluation context (external variables)
context = {
"allowed_values": ["ransack", "ransacklib"],
}
# Initialize ransack objects
parser = Parser(context)
flt = Filter()
# Parse and evaluate the query
query = parser.parse("a.nested_key in allowed_values and (len(b) > 1 or c == 1)")
result = flt.eval(query, data)
print(result) # True
The query checks:
whether
a.nested_key
is inallowed_values
, andwhether
len(b) > 1
orc == 1
— both are true in this case.