Dynamically creating page definitions in Cherrypy
Posted
by Hugh
on Stack Overflow
See other posts from Stack Overflow
or by Hugh
Published on 2010-03-23T10:51:28Z
Indexed on
2010/03/23
10:53 UTC
Read the original article
Hit count: 291
Hi,
I've been looking around the CherryPy documentation, but can't quite get my head around what I want to do. I suspect it might be more of a Python thing than a CherryPy thing...
My current class looks something like this:
import managerUtils
class WebManager:
def A(self, **kwds):
return managerUtils.runAction("A", kwds)
A.enabled = True
def B(self, **kwds):
return managerUtils.runAction("B", kwds)
B.enabled = True
def C(self, **kwds):
return managerUtils.runAction("C", kwds)
C.enabled = True
Obviously there's a lot of repetition in here.
in managerUtils.py, I have a dict that's something like:
actions = {'A': functionToRunForA,
'B': functionToRunForB,
'C': functionToRunForC}
Okay, so that's a slightly simplistic view of it, but I'm sure you get the idea.
I want to be able to do something like:
import managerUtils
class WebManager:
def __init__(self):
for action in managerUtils.actions:
f = registerFunction(action)
f.enabled = True
Any ideas of how to do this?
© Stack Overflow or respective owner