Skip to content

urlpattern/python-urlpattern

Repository files navigation

URL Pattern

PyPI - Version PyPI - Python Version Ruff CI

An implementation of the URL Pattern Standard for Python written in Rust.

Introduction

The URL Pattern Standard is a web standard for URL pattern matching. It is useful on the server side when serving different pages based on the URL (a.k.a. routing). It provides pattern matching syntax like /users/:id, similar to route parameters in Express or Path-to-RegExp. You can use it as a foundation to build your own web server or framework.

It's a thin wrapper of denoland/rust-urlpattern with PyO3 + Maturin.

Installation

On Linux/UNIX or macOS:

pip install urlpattern

On Windows:

py -m pip install urlpattern

Usage

Check urlpattern.pyi.

Examples

For various usage examples, refer to Chrome for Developers or MDN (you may need to convert JavaScript into Python).

test

from urlpattern import URLPattern

pattern = URLPattern("https://example.com/admin/*")
print(pattern.test("https://example.com/admin/main/"))  # output: True
print(pattern.test("https://example.com/main/"))  # output: False

exec

from urlpattern import URLPattern

pattern = URLPattern({"pathname": "/users/:id/"})
result = pattern.exec({"pathname": "/users/4163/"})
print(result["pathname"]["groups"]["id"])  # output: 4163

ignoreCase

from urlpattern import URLPattern

pattern = URLPattern("https://example.com/test")
print(pattern.test("https://example.com/test"))  # output: True
print(pattern.test("https://example.com/TeST"))  # output: False

pattern = URLPattern("https://example.com/test", {"ignoreCase": True})
print(pattern.test("https://example.com/test"))  # output: True
print(pattern.test("https://example.com/TeST"))  # output: True

Simple WSGI app

from urlpattern import URLPattern
from wsgiref.simple_server import make_server


user_id_pattern = URLPattern({"pathname": "/users/:id"})


def app(environ, start_response):
    path = environ["PATH_INFO"]

    if result := user_id_pattern.exec({"pathname": path}):
        user_id = result["pathname"]["groups"]["id"]
        start_response("200 OK", [("Content-Type", "text/plain; charset=utf-8")])
        return [f"{user_id=}".encode()]


with make_server("", 8000, app) as httpd:
    httpd.serve_forever()

Limitations

Due to limitations in the dependency denoland/rust-urlpattern, it may not support all features specified in the standard.

Check the pytest.skip in tests/test_lib.py.

About

An implementation of the URL Pattern Standard for Python written in Rust.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •