Browse files


  • Loading branch information...
1 parent 98f43ee commit b9531ecff71152296547a2f5820f8be559474e59 anonymous committed with anandology Jul 17, 2006
Showing with 49 additions and 1 deletion.
  1. +49 −1
@@ -50,10 +50,21 @@ Of course, this is a simple example that leaves a lot to be desired (handling at
**AaronSw writes:** That's a clever idea. One improvement might be to have the classes inherit from a class with the metaclass set, so you don't have that unsightly `__metaclass__` every time.
+**xunil writes:** Yeah, I went w/ this approach in the end since I found that I wanted each action class to inherit some common functionality anyway. You can [view the source]( for [my incomplete site]( which uses a metaclass and decorators.
* * *
May be it's better to do it with decorators?
+**xunil writes:** I originally tried to use decorators to do this since I was already familiar w/ them, but unless I'm missing something, decorators don't work on classes eg.
+ @action("/")
+ class Default:
+ def GET(self):
+ pass
+That won't work. [The PEP for decorators]( describes it, but apparently it was never incorporated into the language (perhaps b/c metaclasses are available and do the same thing).
* * *
I couldn't figure out how to get this to work with URL classes in separate files, so I did something a little different, based on [this Python Cookbook recipe](
@@ -81,4 +92,41 @@ And then e.g. (in the same directory) would be:
def GET(self, name):
i = web.input(times=1)
if not name: name = 'world' for c in xrange(int(i.times)): print 'Hello,', name+'!'
-Is this a terrible way to do it?
+Is this a terrible way to do it?
+* * *
+**xunil writes:** This is not altogether a bad idea, but it again decouples the URL information from the class, making it a module-level tuple or list. You could combine your importing logic w/ my metaclass idea, actually, and achieve the same thing as me eg.
+ import os, web
+ for aaa in os.listdir(os.getcwd()):
+ module_name, ext = os.path.splitext(aaa)
+ if module_name.startswith('cgi_') and ext == '.py':
+ module = __import__(module_name)
+ if __name__ == "__main__":
+ import metaclass
+ urls = [ ]
+ class ActionMetaClass(type):
+ def __init__(klass, name, bases, attrs):
+ urls.append(attrs["url"])
+ urls.append("%s.%s" % (klass.__module__, name))
+ import web
+ from metaclass import ActionMetaClass
+ class hello:
+ __metaclass__ = ActionMetaClass
+ url = '/(.*)'
+ def GET(self, name):
+ i = web.input(times=1)
+ if not name: name = 'world' for c in xrange(int(i.times)): print 'Hello,', name+'!'
+* * *

0 comments on commit b9531ec

Please sign in to comment.