Can I use decorators to manipulate return type or create methods?
WakeBdr
bbull at optiosoftware.com
Thu Oct 19 09:43:49 EDT 2006
I'm writing a class that will query a database for some data and return
the result to the caller. I need to be able to return the result of
the query in several different ways: list, xml, dictionary, etc. I was
wondering if I can use decorators to accomplish this.
For instance, I have the following method
def getUsers(self, params):
return users.query(dbc)
To get the appropriate return types, I also have these methods. I have
these convenience methods for every query method in my class.
def getUsersAsXML(self, params):
return self._toXML(self.getUsers(params))
def getUsersAsDict(self, params):
return self._toDict(self.getUsers(params))
def getUsersAsList(self, params):
return self._toList(self.getUsers(params))
Instead of creating these three methods for every query method, is
there a way to use decorators to manipulate the return type. I'd still
like to have the caller use getUsersAsXML, I just don't want to write
the AsXML methods for every query method. So the decorator would
essentially create the convenience methods instead of me coding them.
One solution that I don't want to use is passing a variable into the
query method that determines the return type. This is what I don't
want to do.
def getUsers(self, params, returnType):
Any ideas on how I can accomplish this?
thanks
More information about the Python-list
mailing list