In CherryPy, I can raise cherrypy.HTTPError(code, message)
when something goes wrong, or I return string
when it all works out.
What I am looking for now, is how can I create a class, so that I can do this:
class MyOwnResultClass:
foo = None
def __init__(self, foo):
self.foo = foo
def __str__(self):
return "Result: {f}".format(f=self.foo)
class cherrypystuff(object):
@cherrypy.expose
def index(self):
return MyOwnResultClass("f")
This fails with the error
TypeError: iteration over non-sequence
And I have to use return str(MyOwnResultClass("f")
.
I dived a little more into the error message, and apparently, CherryPy wants an iterable object as return type, if not a string.
So, the solution is quite simple: make the MyOwnResultClass
iterable:
class MyOwnResultClass:
foo = None
def __init__(self, foo):
self.foo = foo
def __iter__(self):
yield "Result: {f}".format(f=self.foo)
Update after seeing the talk suggested by @webKnjaZ, I refactored to this:
def MyOwnResult(foo):
yield "Result: {f}".format(f=foo)