python flask 如何将动态参数传递给装饰器
python flask how to pass a dynamic parameter to a decorator
我正在使用 python flask 框架。我写了一个需要参数的装饰器,这个参数是动态的。
我的装饰器如下所示,将获得一个密钥,并使用密钥从 redis 中获取数据。
def redis_hash_shop_style(key):
def fn_wrapper(f):
@wraps(f)
def decorated_function(*args, **kwargs):
data = redis_hash(key)
return data
return decorated_function
return fn_wrapper
我class使用这个装饰器,代码如下
class ShopAreaAndStyleListAPI(Resource):
@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
def get(self):
# if not found from redis, query from mysql
pass
如你所见,我的装饰器需要一个名为 key
的参数,我这样传递密钥
@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
g.city.id
会得到城市的id,如果一切正常的话,key就是这样
shop_100_style
但我得到了错误:
class ShopAreaAndStyleListAPI(Resource):
File "xx.py", line 659, in ShopAreaAndStyleListAPI
@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/flask/globals.py", line 44, in _lookup_app_object
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that
needed to interface with the current application object in a way.
To solve this set up an application context with app.app_context().
See the documentation for more information.
我很困惑,在flask中,如何将动态参数传递给装饰器?
谢谢。
如果我们检查 flask application global、flask.g
的文档,它说:
To share data that is valid for one request only from one function to another, a global variable is not good enough because it would break in threaded environments. Flask provides you with a special object that ensures it is only valid for the active request and that will return different values for each request.
这是通过使用线程本地代理(在 flask/globals.py
中)实现的:
g = LocalProxy(partial(_lookup_app_object, 'g'))
我们应该记住的另一件事是 Python 正在 "compile" 阶段执行装饰器的第一遍,在任何请求或 flask
应用程序之外。这意味着当您的应用程序启动时(当您的 class 正在 parsed/decorated 时),key
参数在 flask
请求上下文之外被分配了一个值 'shop_{}_style'.format(g.city.id)
。
但是我们可以通过使用惰性代理轻松延迟对 flask.g
的访问,该代理仅在使用时通过回调函数获取值。让我们使用已经与 flask
捆绑在一起的 werkzeug.local.LocalProxy
:
from werkzeug.local import LocalProxy
class ShopAreaAndStyleListAPI(Resource):
@redis_hash_shop_style(key=LocalProxy(lambda: 'shop_{}_style'.format(g.city.id)))
def get(self):
# if not found from redis, query from mysql
pass
通常(对于非flask
或非werkzeug
应用程序),我们可以使用ProxyTypes
包中的类似LazyProxy
。
与此无关,您还需要修复 redis_hash_shop_style
装饰器,使其不仅从 redis
获取,而且还更新(或创建)过时(或非存在),通过在适当的时候调用包装的 f()
。
我正在使用 python flask 框架。我写了一个需要参数的装饰器,这个参数是动态的。
我的装饰器如下所示,将获得一个密钥,并使用密钥从 redis 中获取数据。
def redis_hash_shop_style(key):
def fn_wrapper(f):
@wraps(f)
def decorated_function(*args, **kwargs):
data = redis_hash(key)
return data
return decorated_function
return fn_wrapper
我class使用这个装饰器,代码如下
class ShopAreaAndStyleListAPI(Resource):
@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
def get(self):
# if not found from redis, query from mysql
pass
如你所见,我的装饰器需要一个名为 key
的参数,我这样传递密钥
@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
g.city.id
会得到城市的id,如果一切正常的话,key就是这样
shop_100_style
但我得到了错误:
class ShopAreaAndStyleListAPI(Resource):
File "xx.py", line 659, in ShopAreaAndStyleListAPI
@redis_hash_shop_style(key='shop_{}_style'.format(g.city.id))
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/Users/xx/.virtualenvs/yy/lib/python2.7/site-packages/flask/globals.py", line 44, in _lookup_app_object
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that
needed to interface with the current application object in a way.
To solve this set up an application context with app.app_context().
See the documentation for more information.
我很困惑,在flask中,如何将动态参数传递给装饰器?
谢谢。
如果我们检查 flask application global、flask.g
的文档,它说:
To share data that is valid for one request only from one function to another, a global variable is not good enough because it would break in threaded environments. Flask provides you with a special object that ensures it is only valid for the active request and that will return different values for each request.
这是通过使用线程本地代理(在 flask/globals.py
中)实现的:
g = LocalProxy(partial(_lookup_app_object, 'g'))
我们应该记住的另一件事是 Python 正在 "compile" 阶段执行装饰器的第一遍,在任何请求或 flask
应用程序之外。这意味着当您的应用程序启动时(当您的 class 正在 parsed/decorated 时),key
参数在 flask
请求上下文之外被分配了一个值 'shop_{}_style'.format(g.city.id)
。
但是我们可以通过使用惰性代理轻松延迟对 flask.g
的访问,该代理仅在使用时通过回调函数获取值。让我们使用已经与 flask
捆绑在一起的 werkzeug.local.LocalProxy
:
from werkzeug.local import LocalProxy
class ShopAreaAndStyleListAPI(Resource):
@redis_hash_shop_style(key=LocalProxy(lambda: 'shop_{}_style'.format(g.city.id)))
def get(self):
# if not found from redis, query from mysql
pass
通常(对于非flask
或非werkzeug
应用程序),我们可以使用ProxyTypes
包中的类似LazyProxy
。
与此无关,您还需要修复 redis_hash_shop_style
装饰器,使其不仅从 redis
获取,而且还更新(或创建)过时(或非存在),通过在适当的时候调用包装的 f()
。