1

I am getting started with my first cherrypy app. I am using the example from the docs http://docs.cherrypy.org/dev/progguide/REST.html

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)

I got an error with the lambda method on the following line:

html_item = lambda (name,value): '<div>{name}:{value}</div>'.format(\*\*vars())

and changed it to the following after reading Python 3.2 Lambda Syntax Error

html_item = lambda nv: '<div>{nv[0]}:{nv[1]}</div>'.format(\*\*vars())

But now I'm getting "SyntaxError: unexpected character after line continuation character" linking to the end of the above line .format(\*\*vars()).

What is causing that?

I am running Python 3.2 and CherryPy 3.2.2

Community
  • 1
  • 1
xylar
  • 7,433
  • 17
  • 55
  • 100

1 Answers1

3

Remove the backslashes. The correct syntax for keyword parameter expansions is double asterisks:

html_item = lambda nv: '<div>{nv[0]}:{nv[1]}</div>'.format(**vars())

This looks like a bug in the CherryPy documentation rendering.

The \ backslash (outside string literals) is used to signal a line continuation and is only allowed at the end of a line to tell Python to ignore the newline:

somevar = some_function_call(arg1, arg2) + \
          some_other_function_call(arg3, arg4)

The syntax is not recommended (you should use parenthesis instead), but that is what Python was expecting to see here instead of a asterisk.

Quick demo showing the exception is indeed caused by the backslashes:

>>> test = dict(foo='bar')
>>> '{foo}'.format(\*\*test)
  File "<stdin>", line 1
    '{foo}'.format(\*\*test)
                           ^
SyntaxError: unexpected character after line continuation character
>>> '{foo}'.format(**test)
'bar'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343