Routing
Baby step has been made with http://kua.readthedocs.io/en/latest/ which is very simple, but also has some limitations (no way yet to have a dynamic part of the url containing a "/", see https://github.com/nitely/kua/issues/2).
Let's consider other python libs, to challenge kua choice.
Need
- Very simple route to
anything
resolver. - Needs to be fast.
- Allow dynamic parts.
- Allow "/" in dynamic parts.
- typed dynamic parts is a plus (eg. int:xxx)
- reverse is a plus (eg. url_for())
Libraries
Kua
https://github.com/nitely/kua/issues/2
- maintained: yes
- sample:
from kua import routes
routes_ = routes.Routes()
routes_.add('api/:foo', {'GET': my_get_controller})
route = routes_.match('api/hello-world')
route.params
# {'foo': 'hello-world'}
route.anything
# {'GET': my_get_controller}
- pros: simple and agnostic
- cons: not (yet) possible to have "/" in dynamic parts
wheeze.routing
https://bitbucket.org/akorn/wheezy.routing / http://wheezyrouting.readthedocs.io/en/latest/examples.html
- maintained: no
- sample:
from wheezy.routing import PathRouter
r = PathRouter()
r.add_routes([
('/', some_func),
('/{any}', another_func)
])
handler, kwargs = r.match(environ['PATH_INFO'])
- pros: seems fast (according to benchmark below) and powerful
- cons: opinionated API (WSGI oriented); source code overcomplex
Routes
https://routes.readthedocs.io/en/latest/
- maintained: seems so
- sample:
# Setup a mapper
from routes import Mapper
map = Mapper()
map.connect(None, "/error/{action}/{id}", controller="error")
map.connect("home", "/", controller="main", action="index")
# Match a URL, returns a dict or None if no match
result = map.match('/error/myapp/4')
# result == {'controller': 'error', 'action': 'myapp', 'id': '4'}
- pros: nice API
- cons: seems slow
Avenue
https://github.com/CorverDevelopment/Avenue/
- maintained: not sure
from avenue import Avenue
router = Avenue()
@router.attach(path='/', method='GET')
def hello_world():
return 'Hallo world!'
route = {
'path': '/',
'method': 'GET',
}
assert router.solve(**route) == 'Hallo world!'
- cons: opinionated API (decorator, method…)
Traject
https://pypi.python.org/pypi/traject/0.10.1
- last changes: 2010
Bamboo
https://pypi.python.org/pypi/bamboo/0.2.10
- last changes: 5 years ago
Routing
https://pypi.python.org/pypi/routing/0.2.0
- last changes: 3 years ago
- cons: opinionated app based API (app = App(), app.route…)
Biro
https://github.com/zweifisch/biro
- last changes: 3 years ago
from biro import get, path_for, match
@get('/article')
def list_article():
pass
@get('/article/<article_id>')
def show_article():
pass
path_for(show_article, article_id=618) # /article/618
path_for(list_article, limit=10) # /article?limit=10
match('GET', "/article/309") # (article, {"article_id": "309"})
match('POST', "/article/309") # (None, None)
decoroute
https://bitbucket.org/sevkin/decoroute/
- last updated: 2012
routr
http://routr.readthedocs.io/en/latest/
- maintained: 2014
- python3: 3.3
from routr import route, GET, POST
from routr.schema import form, String
def create_user(username=None):
...
def get_user(id):
...
routes = route("/users",
route(POST, "/", form(username=String), create_user),
route(GET, "/{id:int}", get_user)
)
selector
https://github.com/lukearno/selector/
- last updated: 2014
- cons: REST oriented
import selector
app = selector.Selector()
app.add('/resource/{id}', GET=wsgi_handler)
destination
https://github.com/futursolo/destination
- maintained: yes
- cons: regex based
decorouter
https://github.com/dhain/decorouter
- last updated: 2011
- cons: WSGI oriented
organic
https://pypi.python.org/pypi/organic
- last updated: 2013
roadmap
https://github.com/eamartin/roadmap
- last update: 7 years ago
aio-routes
https://github.com/tailhook/aio-routes
- last updated: 3 years ago
- cons: more a tiny web server than a pure routing engine
web.dispatch.route (part of WebCore)
https://github.com/marrow/web.dispatch.route
- cons: complex API