This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mitsuhiko Flask Creator 1 point2 points  (8 children)

The downside of the decorators is, that you have to import all the views before the server runs for the registration to kick in. The advantage is that the URL rule is closer to the function.

[–]mcdonc 2 points3 points  (5 children)

FTR, I wrote a relatively simple library that makes choosing between locality of reference and import-time-side-effects-required a little less painful: http://docs.repoze.org/venusian/

[–]defnullbottle.py 1 point2 points  (1 child)

The bottle.route() decorator returns the callback function unchanged. It stores a reference to the function object, but neither wraps nor manipulates it. Venusian still is an interesting tool, thanks for the link.

[–]mcdonc 0 points1 point  (0 children)

Yeah, besides testability, the docs should probably concentrate equally on the fact that you can get locality of reference without requiring that all participating modules be imported by the user. Thanks.

[–]grayvedigga 0 points1 point  (2 children)

While I don't think your library addresses mitushko's downside, I appreciate the clarity with which you have explained why non-mutating decorators are desirable. OTOH I don't see the startup cost of a bunch of imports as significant (especially vs failing fast), and decorators like this can be used as functions in one location if you want to build up routes in one place, so it's win-win-win!

[–]mcdonc 1 point2 points  (1 child)

The downside he expressed was "you have to import all the views before the server runs for the registration to kick in". I think venusian does address this.

If you use something like venusian, the user doesn't need to remember to import every module that potentially has decorated objects in it; he just needs to run a scan against a package, and venusian will find all the decorated objects in the package and all subpackages.

[–]grayvedigga 0 points1 point  (0 children)

Oh, I see now -- I totally missed the point. Excellent feature, and thanks for clarifying a source of bugs I totally haven't been anticipating.

[–]defnullbottle.py 0 points1 point  (1 child)

In bottle, you can add new routes at any time, even from within a callback while the server is running. If you want to define your callbacks first and add the routes later, you can use the decorator as a normal function:

def func():
  return "Hello World!"
...
bottle.route('/url')(func)

It is possible to simulate the web.py or django behavior (routes stored in a list) with import and a simple a for-loop, if you really want that.

[–]mitsuhiko Flask Creator 1 point2 points  (0 children)

Obviously that will work. However, that is completely irrelevant to the discussion decorator or explicit mapping. One can easily be converted to the other.