This is a Python binding for https://crates.io/crates/mokaccino.
Mokaccino is a Percolator.
A Percolator is a component that allows the matching of a stream of documents (for instance representing events) against a set of queries (representing specific interests in events).
Install via pip compatible tools as usual.
Example Usage (taken from a test):
def test_percolator():
p = Percolator()
assert p is not None
qids = [
p.add_query(Query.from_kv("name", "sausage")),
p.add_query(Query.from_kprefix("name", "amaz")),
p.add_query(Query.from_kgt("price", 12)),
p.add_query(Query.from_kv("name", "sausage") | Query.from_kgt("price", 12)),
]
assert p.percolate_list(Document()) == []
assert p.percolate_list(Document().with_value("name", "burger")) == []
assert p.percolate_list(Document().with_value("name", "sausage")) == [qids[0], qids[3]]
assert p.percolate_list(Document().with_value("name", "amaz")) == [qids[1]]
assert p.percolate_list(Document().with_value("name", "amazing")) == [qids[1]]
assert p.percolate_list(Document().with_value("name", "amazon")) == [qids[1]]
assert p.percolate_list(Document().with_value("price", "12")) == []
assert p.percolate_list(Document().with_value("price", "13")) == [qids[2], qids[3]]
assert p.percolate_list(
Document().with_value("price", "13").with_value("name", "amazed")
) == [qids[1], qids[2], qids[3]]You can match documents based on their location within an H3 cell.
# Matches documents whose "location" field is inside the given H3 cell
# (or inside any child cell)
q = Query.from_h3in("location", "85283473fffffff")
# Or using parsing:
q = Query.parse("location H3IN 85283473fffffff")If you plan to have free formed queries, you can use query parsing to build queries:
qids = [
p.add_query(Query.parse("name:sausage")),
p.add_query(Query.parse("name:amaz*")),
p.add_query(Query.parse("price>12")),
p.add_query(Query.parse("name:sausage OR price>12")),
]Query parsing works as you expect, with boolean AND, OR and NOT and ( ) to
work around precedence.
Non-word values can be enclosed in "s (for example field:"non word value") and the
escape character is \.
More extensive documentation will be provided, but in the meanwhile, have a look at the unit tests, which cover everything you can do with this:
https://github.com/jeteve/mokaccino_py/tree/main/tests
This uses uv/uvx to handle the python side of things
- Prepare the venv
uv venv --python 3.13
uv sync --extra dev
- Compile everything using maturin
# To regenerate the mokaccino.pyi stub:
cargo run -F stub-gen --bin stub_gen
uvx maturin develop- Run some examples or unit tests
uv sync --extra dev
pytest
uv run examples/...In development, loop through 3 and 2:
uvx maturin develop && pytestThis is developed at https://github.com/jeteve/mokaccino_py