I am trying to write a simple (lightweight) RESTful server in Python. I have come across the following code from Google:
import web
import json
from mimerender import mimerender
render_xml = lambda message: '<message>%s</message>'%message
render_json = lambda **args: json.dumps(args)
render_html = lambda message: '<html><body>%s</body></html>'%message
render_txt = lambda message: message
urls = (
'/(.*)', 'greet'
)
app = web.application(urls, globals())
class greet:
@mimerender(
default = 'html',
html = render_html,
xml = render_xml,
json = render_json,
txt = render_txt
)
def GET(self, name):
if not name:
name = 'world'
return {'message': 'Hello, ' + name + '!'}
if __name__ == "__main__":
app.run()
I am unfamiliar with the syntax used on the line @mimerender
. This appears to be a a weird combination of a constructor and a function decorator - however, all uses of decorators I have encountered to date are typically written like this:
def foo():
pass
def foobar():
pass
@foo
@pass
def some_other_func():
pass
What does the @mimerender
section of the code mean/do?