Python 在cherrypy中将函数传递给装饰器

Python 在cherrypy中将函数传递给装饰器,python,cherrypy,Python,Cherrypy,如何将index()传递给名为uppercase()的decorator函数。我希望从index()传递的值大写 我得到这个错误: 500 Internal Server Error The server encountered an unexpected condition which prevented it from fulfilling the request. Traceback (most recent call last): File "/home/user/.local/l

如何将
index()
传递给名为
uppercase()
的decorator函数。我希望从
index()
传递的值大写

我得到这个错误:

500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request.

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 638, in respond
    self._do_respond(path_info)
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 701, in _do_respond
    self.hooks.run('before_finalize')
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 95, in run
    self.run_hooks(iter(sorted(self[point])))
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 117, in run_hooks
    hook()
  File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 65, in __call__
    return self.callback(**self.kwargs)
TypeError: uppercase() missing 1 required positional argument: 'func'

import cherrypy
来自cherrypy导入工具
@cherrypy.tools.register('before_finalize')
def大写字母(func):
def包装器():
原始结果=func()
修改的结果=原始结果。上限()
返回修改后的结果
返回包装器
类HelloWorld(对象):
@樱桃树
@cherrypy.tools.uppercase()
def索引(自):
回答“你好!”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
cherrypy.tools.uppercase=cherrypy.Tool('before_finalize',大写)
cherrypy.quickstart(HelloWorld())

您应该像这样使用装饰器:

@cherrypy.tools.uppercase
def index(self):
    return 'Hello!'
注意
大写后缺少
()
——修饰符隐式接受第一个参数(修饰函数),不需要括号

编辑:

根据,可以使用
cherrypy.tools.register
decorator或
cherrypy.tool
构造函数定义工具。在代码中,您可以定义两次
大写
工具

但是,在您的例子中,
大写
装饰器不需要被定义为
工具
,因为它不是每次都需要作为钩子运行的东西(在完成之前)

因此,您最好将其用作普通Python装饰器,如下所示:

from functools import wraps

# just a plain Python decorator
def uppercase(func):
    @wraps(func) #preserve function attributes, such as its name
    def wrapper(*args):
        original_result = func(*args)
        modified_result = original_result.upper()
        return modified_result
    return wrapper

class HelloWorld(object):
    @cherrypy.expose
    @uppercase # decorated once, the exposed function is now uppercase(index) 
    def index(self):
        return 'Hello!'

您应该像这样使用装饰器:

@cherrypy.tools.uppercase
def index(self):
    return 'Hello!'
注意
大写后缺少
()
——修饰符隐式接受第一个参数(修饰函数),不需要括号

编辑:

根据,可以使用
cherrypy.tools.register
decorator或
cherrypy.tool
构造函数定义工具。在代码中,您可以定义两次
大写
工具

但是,在您的例子中,
大写
装饰器不需要被定义为
工具
,因为它不是每次都需要作为钩子运行的东西(在完成之前)

因此,您最好将其用作普通Python装饰器,如下所示:

from functools import wraps

# just a plain Python decorator
def uppercase(func):
    @wraps(func) #preserve function attributes, such as its name
    def wrapper(*args):
        original_result = func(*args)
        modified_result = original_result.upper()
        return modified_result
    return wrapper

class HelloWorld(object):
    @cherrypy.expose
    @uppercase # decorated once, the exposed function is now uppercase(index) 
    def index(self):
        return 'Hello!'

请将完整的错误回溯添加到您的问题中!从
@cherrypy.tools.uppercase()。您在没有参数的情况下显式调用它,这就是您看到错误“缺少1个必需的位置参数”的原因;您必须使用关键字参数。请将完整的错误回溯添加到您的问题中!从
@cherrypy.tools.uppercase()。您在没有参数的情况下显式调用它,这就是您看到错误“缺少1个必需的位置参数”的原因;您必须使用关键字参数。我尝试过,但得到以下错误类型错误:“大写”工具不接受位置参数;您必须使用关键字参数。@jeril I更新了答案,请检查此方法是否有用。否..使用普通python装饰器尝试。。TypeError:wrapper()接受0个位置参数,但1个位置参数无效given@jeril当然,糟糕的是,包装器假定修饰函数不接受参数,但它确实接受“self”。您可以将*args添加到包装器的定义和内部func的调用中;您必须使用关键字参数。@jeril I更新了答案,请检查此方法是否有用。否..使用普通python装饰器尝试。。TypeError:wrapper()接受0个位置参数,但1个位置参数无效given@jeril当然,糟糕的是,包装器假定修饰函数不接受参数,但它确实接受“self”。您可以将*args添加到包装器的定义和内部func的调用中。