From 57402ad01233b70370153d2360b7ef50d2a3fd2c Mon Sep 17 00:00:00 2001 From: Oz N Tiram Date: Fri, 18 Dec 2020 15:42:49 +0100 Subject: Improve __repr__ of Route The current improvement really helps when using multiple apps. When I start such an application I like logging which routes are available, for example one can do: ``` for r in main_app.routes: print(r) ``` Which previously gave this: ``` ' > hello//<_id:int>/' > <:re:foo>/' > /' > ' > > > > > > ' > > ' .server_static at 0x7f750f937af0>> ' .server_static at 0x7f750f937c10>> ' .server_static at 0x7f750f937d30>> ``` Memory addresses are of very little use (I can only tell which route is which because I mount them to different prefixes (e.g. editor, admin, etc.). This also mathes the directory structure on the file system. However, this is not always the case, some times I mount applications without prefix. With the current patch I can immidietly tell where a route is coming from: ``` apps.front.views:index GET <:re:(de|us)?\/?> apps.front.views:_index_with_param GET /<:re:(de/|us/)?>hello//<_id:int>/ apps.front.views:_index GET /<:re:(de/|us/)?><:re:foo>/ apps.front.views:main GET /<:re:(de/|us/)?>/ apps.front.views:main GET <:re:(de|us)?\/?> tiny.auth:get_login_template GET /admin/login/ tiny.auth:post_login_template POST /admin/login/ apps.admin.views:index GET /admin/ apps.editor.views:editor GET /editor/ apps.editor.views:template_ POST /editor/template/ apps.editor.views:load_tmp_template GET /editor/preview/ apps.editor.views:convert POST /editor/validate-jinja/ __main__:server_static GET /static/front/ __main__:server_static GET /static/admin/ __main__:server_static GET /static/editor/ ``` I think this little patch might be usefull for others too. --- bottle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bottle.py b/bottle.py index 3835a09..7480ad3 100755 --- a/bottle.py +++ b/bottle.py @@ -610,7 +610,7 @@ class Route(object): def __repr__(self): cb = self.get_undecorated_callback() - return '<%s %r %r>' % (self.method, self.rule, cb) + return '<%s:%s %s %s>' % (cb.__module__, cb.__name__, self.method, self.rule) ############################################################################### # Application Object ########################################################### -- cgit v1.2.1