My old do-it-yourself framework tutorial was getting a bit long in the tooth, so I rewrote it to use WebOb. Now: the new do-it-yourself framework.
{ 2008 04 17 }
{ 2008 04 17 }
My old do-it-yourself framework tutorial was getting a bit long in the tooth, so I rewrote it to use WebOb. Now: the new do-it-yourself framework.
Niki Spahiev | 17-Apr-08 at 2:22 am | Permalink
“The return value of __ import __ isn’t very useful”
What’s wrong with module returned by __ import __()?
Roger Erens | 17-Apr-08 at 2:40 am | Permalink
Maybe you could add in the introduction some links on how to obtain and install WSGI and WebOb?
Chris Hoeppner | 17-Apr-08 at 3:32 am | Permalink
This is the kind of stuff I’ve been looking for to fill my pythonic gap after learning the basics and before banging my head on the wall with anything too big =)
Carlo Cabanilla | 17-Apr-08 at 7:01 am | Permalink
Hey Ian,
I’ve been playing with webob a lot lately and I gotta say that it’s great! In all my past apps, I’ve been trying to model requests/responses in a nice Pythonic and RESTful way but never quite made it, so webob is just what I’ve been looking for.
I was wondering if there was any plans on letting webob Request objects make actual HTTP calls, returning webob Responses? I wrote a wrapper class to do it, but I think it’d make sense to be part of the Request object. If it’s something you’d be open to, I’d like to contribute some code.
.Carlo
Ian Bicking | 17-Apr-08 at 10:14 am | Permalink
Niki:
__import__returns the first segment of whatever you import. So if you do__import__('mypackage.mymodule')it returnsmypackage. Just getting the module fromsys.modulesis, I’ve found, the easiest way of getting at the module that was actually imported.Carlo: yes, you can do that. To do it you need to pass a proxy application to
req.get_response(). There’s one in WSGIProxy, inwsgiproxy.exactproxy.proxy_exact_request. So you can do:proxy_exact_requestis named as such because it has no options, but reads the exact information the request contains (including stuff like SERVER_NAME so you can send a request not by DNS).Chris Hoeppner | 18-Apr-08 at 3:51 am | Permalink
Ian & Niki:
For
__import__, you can also give the 4th argument as a list with a single empty string. This is the “context” argument, meaning it acts likefrom x import y, context being the x bit. Giving it an empty string makes it return the last bit of the first argument.For example:
returns module, not some.
Chris Hoeppner | 18-Apr-08 at 3:58 am | Permalink
Sorry for double posting. This is actually what I came to post.
I’m not sure wether you’ve read Diving into Python. The way code is commented in that book is absolutely genius. You see a big piece of code, just like yours. And below it you find a numbered explanation of every relevant line or group of lines.
I’m saying this because I had my little problems understanding what exactly your code does. Like the routing templating thing. I thought “Okay. This turns our nice routing syntax into actual regular expressions to use elsewhere. But how does he do it?”.
Jeff Hammel | 20-Apr-08 at 10:05 am | Permalink
I agree that writing a framework is a useful exercise, and think that a language should provide the one true framework as a mis-notion for people that want an out of the box solution to a complex problem.
I’ve found webob both very useful and fun to use in writing simple RESTful apps. I’ve written a paster template for a simple view with webob that might be useful to some, at least for reference: https://svn.openplans.org/svn/standalone/webob_view/trunk/ This could be used or be modified to be used with a simple framework/routing system as presented in the tutorial — just have a bunch of views with a route for each == website.