webob decorator idea

2 views
Skip to first unread message

Ian Bicking

unread,
Jun 2, 2009, 2:07:18 AM6/2/09
to Paste Users
Is there anything really wrong about a decorator that looks roughly like this?

def wsgify(func):
    def replacement(req, *args, **kw):
        if type(req) is dict:
            environ = req
            req = Request(environ)
            start_response = args[0]
            try:
                resp = func(req)
            except exc.HTTPException, resp:
                pass
            return resp(environ, start_response)
        else:
            return func(req, *args, **kw)
    return replacement

I.e., it's a WSGI application and an undecorated function, and maybe it could seamlessly support WSGI 2 too.

--
Ian Bicking  |  http://blog.ianbicking.org

Sergey Schetinin

unread,
Jun 2, 2009, 7:40:03 AM6/2/09
to Ian Bicking, Paste Users
I think this is much better, but still think that this is a compromise
where no compromise is necessary. I had support for the same thing in
my decorator -- but it turned out I never needed to use it so I took
it out. It seems entirely possible to me that you're overestimating
the hassle associated with having to use a separate method in cases
when you need additional arguments etc.

(BTW, I think with this change checks for .wsgi_app in get_response in
dec branch can be taken out as well)
--
Best Regards,
Sergey Schetinin

http://s3bk.com/ -- S3 Backup
http://word-to-html.com/ -- Word to HTML Converter

Ian Bicking

unread,
Jun 3, 2009, 5:25:11 PM6/3/09
to Sergey Schetinin, Paste Users
On Tue, Jun 2, 2009 at 6:40 AM, Sergey Schetinin <mal...@gmail.com> wrote:
I think this is much better, but still think that this is a compromise
where no compromise is necessary. I had support for the same thing in
my decorator -- but it turned out I never needed to use it so I took
it out. It seems entirely possible to me that you're overestimating
the hassle associated with having to use a separate method in cases
when you need additional arguments etc.

Well, it's a compromise that at least works given the differing opinion between the two of us.  I'll probably also rethink the whole module too as it should simplify a bunch of things.


(BTW, I think with this change checks for .wsgi_app in get_response in
dec branch can be taken out as well)

Yeah, I made that change locally but I have some mess I need to straighten before I commit.
 
  Ian


Sergey Schetinin

unread,
Jun 3, 2009, 6:04:23 PM6/3/09
to Ian Bicking, Paste Users
On 2009-06-04, Ian Bicking <ia...@colorstudy.com> wrote:
> On Tue, Jun 2, 2009 at 6:40 AM, Sergey Schetinin <mal...@gmail.com> wrote:
>
> > I think this is much better, but still think that this is a compromise
> > where no compromise is necessary. I had support for the same thing in
> > my decorator -- but it turned out I never needed to use it so I took
> > it out. It seems entirely possible to me that you're overestimating
> > the hassle associated with having to use a separate method in cases
> > when you need additional arguments etc.
> >
>
> Well, it's a compromise that at least works given the differing opinion
> between the two of us. I'll probably also rethink the whole module too as
> it should simplify a bunch of things.

Sure. I'd prefer to use components written with this dual-interface
decorator to the one proposed before, however I'm pretty sure I'll
stick to webob_wrap for my own stuff simply because I like *not*
having any features that wsgify adds. Simpler stuff makes thinking
about things easier and having so many ways to generate response seems
to encourage messy code (reminds me of pyzen). So what I'm saying is
that I'm glad it would be easier to use stuff as WSGI without knowing
anything else about it, and that's all that matters to me at this
point, so agreement reached. :)

BTW, I wonder if you will be taking the wsgify.reverse out?


> >
> > (BTW, I think with this change checks for .wsgi_app in get_response in
> > dec branch can be taken out as well)
> >
> >
> >
> >
>
> Yeah, I made that change locally but I have some mess I need to straighten
> before I commit.
>
> Ian
>
>
>

Kumar McMillan

unread,
Jun 3, 2009, 6:47:14 PM6/3/09
to Ian Bicking, Paste Users
On Tue, Jun 2, 2009 at 1:07 AM, Ian Bicking <ia...@colorstudy.com> wrote:
> Is there anything really wrong about a decorator that looks roughly like
> this?
>
> def wsgify(func):
>     def replacement(req, *args, **kw):
>         if type(req) is dict:
>             environ = req
>             req = Request(environ)
>             start_response = args[0]
>             try:
>                 resp = func(req)
>             except exc.HTTPException, resp:

will this except clause work for Python 2.4? Sad that I have to ask.

Ian Bicking

unread,
Jun 3, 2009, 6:50:18 PM6/3/09
to Kumar McMillan, Paste Users
On Wed, Jun 3, 2009 at 5:47 PM, Kumar McMillan <kumar.m...@gmail.com> wrote:
On Tue, Jun 2, 2009 at 1:07 AM, Ian Bicking <ia...@colorstudy.com> wrote:
> Is there anything really wrong about a decorator that looks roughly like
> this?
>
> def wsgify(func):
>     def replacement(req, *args, **kw):
>         if type(req) is dict:
>             environ = req
>             req = Request(environ)
>             start_response = args[0]
>             try:
>                 resp = func(req)
>             except exc.HTTPException, resp:

will this except clause work for Python 2.4?  Sad that I have to ask.

Yeah, HTTPException is always an exception, it's just things like HTTPNotFound that are not exception objects in Python 2.4.

Ian Bicking

unread,
Jun 10, 2009, 2:00:04 PM6/10/09
to Paste Users
OK, one more webob decorator idea: making the decorator an attribute
of the Request class. This is what werkzeug does, which means that
it's very natural to use a subclass of Request. In werkzeug it is
called application, so it's like:

import webob
@webob.Request.application
def myapp(req):
return webob.Response(...)

Any thoughts on this? Request is of course a pretty big class
already, but then as a result this isn't proportionally much of an
increase in size ;) Middleware would work the same way.

Ian

Sergey Schetinin

unread,
Jun 10, 2009, 2:32:32 PM6/10/09
to Ian Bicking, Paste Users

Cons: It looks a bit ugly when no subclass is needed, "request
application" doesn't sound right and subclassing the decorator itself
is harder this way.

Overall I think these things deserve to be in their own / global namespace.

Also, nothing wrong with @wsgify(Request2) IMO.

Reply all
Reply to author
Forward
0 new messages