python-matchit is a Python binding for the matchit Rust library.
matchit is a high-performance URL/path routing library. It builds a radix tree (compressed trie) from route patterns and finds the best match for a given path in logarithmic time. This makes python-matchit a very fast choice for HTTP routers, URL dispatchers, or any task where you need to match many patterns against paths.
pip install python-matchitfrommatchitimportRouterrouter=Router[str]()
router.insert("/users/{id}", "test_id")
res=router.at("/users/123")
assertres.value=="test_id"assertres.params== {"id": "123"}
try:
router.at("/noway")
exceptLookupError:
print("Not found as expected")Router() without type argument also works.
uv syncBuild and install the package in editable mode, then run tests:
maturin develop --uv
uv run python test.pyNote: use
maturin develop --uv, notuv run maturin develop. The latter runs maturin without telling it to useuvfor installation, which may result in a non-editable install.
To update the underlying Rust matchit library:
Check matchit release notes for breaking changes.
Update the version in
Cargo.toml:[dependencies] matchit = "0.10.0"
Update
Cargo.lock:cargo update -p matchit
Adapt
src/lib.rsif thematchitAPI changed.Rebuild and test:
maturin develop --uv uv run python test.py
maturin build --releaseThe built wheel will be placed in dist/.