【发布时间】:2023-03-17 03:34:01
【问题描述】:
我正在尝试在 python 中创建一个通用函数生命周期处理程序。 工作简介:
- 记录签名和返回值。
- 在重试或不重试的情况下处理异常。
- 在出现异常时提供清理。
我在清理时遇到的问题如下: 基于函数的装饰器:
def handler(exception=Exception,cleanup=None):
def func_wrapper(func):
def wrapper(*args,**kwargs):
response=None
try:
print(args)
response=func(*args,**kwargs)
except exception as ex:
print(f'Exception occurred:{str(ex)}\nCleaning up resources')
#Passing the object for cleanup, it fails for class based decorators as it does not passes self as argument
cleanup(args[0])
return response
return wrapper
return func_wrapper
应该清理的数据存储在类实例中,并根据提供的方法进行清理。 例如:
-
使用第三方 API 存储一些信息。
-
如果出现异常,传递的清理操作将调用 删除 API。
class Test: def __init__(self,data): self.main_data=data @handler(exception=Exception,cleanup=lambda x:print(f"Cleaning data:{x.main_data}")) def test_data(self): print(f'Data is :{self.main_data}') i=1/0
输出:
Exception occurred:division by zero
Cleaning up resources
Cleaning:John Doe
我更倾向于基于类的装饰器。
class LifeCycleHandler:
def __init__(self,*,func=None,exception=Exception,cleanup=None):
self.__exception=exception
self.__cleanup=cleanup
self.__func=func
def __call__(self,*args,**kwargs):
response=None
try:
print(args)
response=self.__func(*args,**kwargs)
except self.__exception as ex:
print(f'Exception occurred:{str(ex)}\n cleaning up resources')
#Passing the object for cleanup
self.__cleanup(args[0])
return response
def lifecycle_handler(exception=Exception,cleanup=None):
def wrapper(func):
response=LifeCycleHandler(func=func,exception=exception,cleanup=cleanup)
return response
return wrapper
使用具有类似功能的基于类的装饰器,我遇到了以下错误:
()
Exception occurred:test_data() missing 1 required positional argument: 'self'
cleaning up resources
Traceback (most recent call last):
File "test.py", line 27, in __call__
response=self.__func(*args,**kwargs)
TypeError: test_data() missing 1 required positional argument: 'self'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 54, in <module>
test.test_data()
File "test.py", line 31, in __call__
self.__cleanup(args[0])
IndexError: tuple index out of range
有人可以指导我关于可调用类的参数解释吗?
【问题讨论】:
-
我假设您的
lifecycle_handler不是LifeCycleHandler的方法,对吧?是不是笔误? -
@Ke Zhang:谢谢您的回复,是的,是错字。我已经更新了问题
标签: python-3.x python-decorators