0
我開始使用我的第一個cherrypy應用程序。我使用從文檔的例子http://docs.cherrypy.org/dev/progguide/REST.htmlSyntaxError:使用.format( * * vars())後續字符的意外字符
import cherrypy
class Resource(object):
def __init__(self, content):
self.content = content
exposed = True
def GET(self):
return self.to_html()
def PUT(self):
self.content = self.from_html(cherrypy.request.body.read())
def to_html(self):
html_item = lambda (name,value): '<div>{name}:{value}</div>'.format(\*\*vars())
items = map(html_item, self.content.items())
items = ''.join(items)
return '<html>{items}</html>'.format(**vars())
@staticmethod
def from_html(data):
pattern = re.compile(r'\<div\>(?P<name>.*?)\:(?P<value>.*?)\</div\>')
items = [match.groups() for match in pattern.finditer(data)]
return dict(items)
class ResourceIndex(Resource):
def to_html(self):
html_item = lambda (name,value): '<div><a href="{value}">{name}</a></div>'.format(\*\*vars())
items = map(html_item, self.content.items())
items = ''.join(items)
return '<html>{items}</html>'.format(**vars())
class Root(object):
pass
root = Root()
root.sidewinder = Resource({'color': 'red', 'weight': 176, 'type': 'stable'})
root.teebird = Resource({'color': 'green', 'weight': 173, 'type': 'overstable'})
root.blowfly = Resource({'color': 'purple', 'weight': 169, 'type': 'putter'})
root.resource_index = ResourceIndex({'sidewinder': 'sidewinder', 'teebird': 'teebird', 'blowfly': 'blowfly'})
conf = {
'global': {
'server.socket_host': '0.0.0.0',
'server.socket_port': 8000,
},
'/': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
}
}
cherrypy.quickstart(root, '/', conf)
我對以下行lambda
方法錯誤:
html_item = lambda (name,value): '<div>{name}:{value}</div>'.format(\*\*vars())
和閱讀Python 3.2 Lambda Syntax Error
html_item = lambda nv: '<div>{nv[0]}:{nv[1]}</div>'.format(\*\*vars())
後更改爲以下
但是現在我得到了「SyntaxError:連續字符後的意外字符」,鏈接到上述行的末尾.format(\*\*vars())
。
這是什麼原因造成的?
我正在運行Python 3.2和CherryPy 3.2.2