creating classes with mix-ins
samwyse
samwyse at gmail.com
Mon May 11 17:44:03 EDT 2009
On May 11, 1:16 pm, samwyse <samw... at gmail.com> wrote:
> I'm writing a class that derives it's functionality from mix-ins.
While waiting, I gave a try at using class decorators. Here's what I
came up with:
def add_methods(*m_list, **kwds):
def wrapper(klass):
for m_name in m_list:
def template(self, which, username, password, *args):
if not self.security.isAuthorised(username, password,
which, m_name):
raise Exception('Unauthorised access')
return getattr(self.blog, m_name)(which, *args)
dotted_name = kwds.get('prefix', '') + m_name
template.__name__ = dotted_name
template.__doc__ = dotted_name
setattr(klass, dotted_name, template)
return klass
return wrapper
@add_methods('newPost', 'editPost', 'getPost', 'newMediaObject',
'getCategories', 'getRecentPosts',
prefix='metaWeblog.')
class MetaWeblog(object):
def __init__(self,
securityHandler=SimpleSecurityHandler,
blogHandler=SimpleBlogHandler):
self.security = securityHandler()
self.blog = blogHandler()
if __name__ == '__main__':
server = SimpleXMLRPCServer(("localhost", 8080))
server.register_instance(MetaWeblog())
server.register_introspection_functions()
server.serve_forever()
The problem that I'm having is that when I call newPost,
SimpleBlogHandler.getRecentPosts gets invoked. Apparently add_methods
isn't generating unique templates. I think I need to move 'template'
into another function, but I'm starting to wonder if metaclasses might
work better. Any ideas?
More information about the Python-list
mailing list