Skip to main content

This package provides a tree-oriented URL router. Most URL routers operate by matching a URL against a list of regular expressions until they find one that matches, but this has obvious performance penalties if there are many routes. URLTree is different; routes are stored as a tree, and matching a URL against the set of routes is equivalent to traversing the tree.

The URLTree router supports parameters, and even allows parameter values to be matched against regular expressions or converted by functions. Moreover, as long as these “restrictions” and the variable names are different, several of these parameters can be declared at the same level of a URL tree; this allows a URL with, say, an integer in one location to be mapped to one destination, while a second URL with non-integer values in that same location may be mapped to another destination.

To use URLTree, allocate a URLTree object and use the URLTree.route() method to map a URL pattern to a destination, like so:

mapper = URLTree()
mapper.route("/article", list_articles, "get")
mapper.route("/article", create_article, "post")
mapper.route("/article/{id}", get_article, "get", id=int)
mapper.route("/article/{id}", update_article, "put", id=int)
mapper.route("/article/{id}", delete_article, "delete", id=int)

Upon receiving a request, the destination and the parameters can be retrieved using the URLTree.resolve() method, like so:

# "req.method" is the HTTP method, and "req.url" is the requested
# URL
dest, params = mapper.resolve(req.method, req.url)

Note that URLTree does not interpret the destination; the examples above use callables, but anything can be used here.

Metadata

Release files for urltree 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for urltree 0.1.0
File Size Uploaded
urltree-0.1.0.tar.gz 13.3 kB Details

Release files / urltree-0.1.0.tar.gz

Download URL urltree-0.1.0.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6f04873334e17c5a285d3f41c6add977edc76d38993606f8a398a57617371354
BLAKE2b-256 checksum
How to use checksums
43094c0f4e55adf54b7dbaab47a7a8e8972566dcc8659f75c5ac90205b2055f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page