import time from functools import wraps #不带参数 def requires_auth(f): @wraps(f) def decorated(): print("1111") return f() return decorated # -------装饰器类 class logit(object): def __init__(self, logfile='out.log'): self.logfile = logfile def __call__(self, func): @wraps(func) def wrapped_function(*args, **kwargs): #把func 的参数,原信息,都传递给了装饰器,在装饰器内 可以调用他的参数和传入参数 log_string = func.__name__ + " was called" print(log_string) # 打开logfile并写入 with open(self.logfile, 'a') as opened_file: # 现在将日志打到指定的文件 opened_file.write(log_string + '\n') # 现在,发送一个通知 self.notify() return func(*args, **kwargs) return wrapped_function def notify(self): # logit只打日志,不做别的 pass # ----- 装饰器之间的参数传递 方式1 ---------- # d={'test_13256':r"F:\py学习\1.模块类学习\test1.txt"} # def decr(f): # @wraps(f) # def fun(self,*args): # path=None # for key in d.keys(): # if f.__name__==key: # path=d[key] # f(self,*args.__add__((path,))) # return fun # # def load_data(f): # @wraps(f) # def fun(self,*args): # path=args[0] # file=open(path) # data=file.readlines() # file.close() # f(self,data) # return fun # ----- 装饰器之间的参数传递 方式2 ---------- d={'test_13256':r"F:\py学习\1.模块类学习\test1.txt"} def decr(f): @wraps(f) def fun(self,*args): path=None for key in d.keys(): if f.__name__==key: path=d[key] f(self,data=path) return fun def load_data(f): @wraps(f) def fun(self,data): path=data file=open(path) data=file.readlines() file.close() c="asaaaaa" f(self,data,c) return fun class aaa(): @decr @load_data def test_13256(self,data,c): print(data,c) if __name__ == "__main__": pass t = aaa() t.test_13256() #==============================pytest 之自定义参数化,也是利用了装饰器======================================================= from functools import wraps from interface.utils.yaml_driver import yaml_load from interface.utils.manage import Manage """ 自定义参数化: 依据函数名自动匹配与该函数名相关的唯一数据来源 #!!!!!!!!!!!!!! 测试函数必须有一个 带默认值的位置参数 data="" !!!!!!!!!!!!!! @user_defined_parameteried() def test_add_new_permanent_material(self,data=""): print(data) """ class NotHasFuntionnameError(Exception): def __init__(self): super().__init__() def __str__(self): return "函数-数据映射表中没有找到该函数名 " class user_defined_parameteried(): # 自定义决定是否传参数 def __init__(self, name="", params=[]): self.name = name self.params = params def __call__(self, func): # 有参和无参 if self.name or self.params: if self.name and self.params: data_list=self.params else: raise ValueError else: data_list=self.get_data_by_fun_name(func.__name__) # 函数被装饰时调用 @wraps(func) def warpped_function(*args, **kwargs): for data in data_list: # 元祖需要解包 if type(data) == type(tuple()): func(*data, *args, **kwargs) else: func(data, *args, **kwargs) # 当类方法被装饰时 @wraps(func) def wrapped_class_function(self, *args, **kwargs): for data in data_list: # 元祖需要解包 if type(data) == type(tuple()): func(self, *data, *args, **kwargs) else: func(self, data, *args, **kwargs) # 判断被装饰的函数是 普通函数还是 类中的方法 if func.__qualname__ == func.__name__: return warpped_function else: return wrapped_class_function # 通过函数名。查找与之对应的唯一 数据 def get_data_by_fun_name(self, fun_name): fun_name_ORM_fun_data = Manage().Get_fun_ORM_data() path = fun_name_ORM_fun_data.get(fun_name, "") if path: data_list = yaml_load(path) return data_list else: raise NotHasFuntionnameError
版权声明:本文为nanxue1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。