【Pyhton 】 装饰器


装饰器,带参数装饰器,装饰器中带参数


 1 import time
 2 
 3 # 普通装饰器
 4 '''
 5 def time_decor(func):
 6     def wrapper():
 7         star_time = time.time()
 8         func()
 9         down_time = time.time()
10         print("总耗时为", down_time - star_time)
11 
12     return wrapper
13 
14 @time_decor
15 def wait():
16     time.sleep(2)
17     
18 if __name__ == '__main__':
19     wait()
20 '''

 1 # 被装饰函数带参数
 2 '''
 3 def time_decor(func):
 4     def wrapper(name):
 5         star_time = time.time()
 6         func(name)
 7         down_time = time.time()
 8         print("总耗时为", down_time - star_time)
 9 
10     return wrapper
11 
12 @time_decor
13 def wait(name):
14     print("这是{}带参数的装饰器".format(name))
15     time.sleep(2)
16 
17 
18 if __name__ == '__main__':
19     name = "yanglei"
20     wait(name)

 1 # 带参数的装饰器
 2 
 3 def time_decor(cond):
 4     def external(func):
 5         def wrapper():
 6             if cond == "True":
 7                 star_time = time.time()
 8                 func()
 9                 down_time = time.time()
10                 print("总耗时为", down_time - star_time)
11             else:
12                 print("\033[31;1m这是啥也没干的\033[0m")
13                 func()
14 
15         return wrapper
16 
17     return external
18 
19 
20 @time_decor(cond="False")
21 def wait():
22     time.sleep(2)
23 
24 if __name__ == '__main__':
25     wait()

 1 # 装饰器和被装饰函数都带参数
 2 
 3 def time_decor(cond):
 4     def external(func):
 5         def wrapper(*args, **kwargs):
 6             if cond == "True":
 7                 star_time = time.time()
 8                 func(*args, **kwargs)
 9                 down_time = time.time()
10                 print("总耗时为", down_time - star_time)
11             else:
12                 print("\033[31;1m这是啥也没干的\033[0m")
13                 func()
14 
15         return wrapper
16 
17     return external
18 
19 
20 @time_decor(cond="True")
21 def wait(*args, **kwargs):
22     print(len(args))
23     if len(args) > 1:
24         time.sleep(2)
25         if args[0]==1:
26             print(123)
27     else:
28         print(args)
29 if __name__ == '__main__':
30     name, age, sun = 1, 2, 3
31     wait(name, age, sun)

 1 # 被装饰函数带返回值
 2 def time_decor(func):
 3     def wrapper(*args, **kwargs):
 4         if func(*args, **kwargs) == None:
 5             print("没有返回值的函数对象")
 6         else:
 7 
 8             print("带返回值的函数对象")
 9             res = func(*args, **kwargs)
10             return res
11 
12     return wrapper
13 
14 
15 @time_decor
16 def wait(*args, **kwargs):
17 
18     sun = args[0]+args[1]
19     return sun
20 
21 
22 if __name__ == '__main__':
23     x = 111
24     y = 222
25     a = wait(x,y)
26     print(a)

1  # 计算运行时间的装饰器   
2 def time_decor(func):
3     def wrapper():
4         start_time = time.time()
5         func()
6         down_time = time.time()
7 
8     return wrapper

 1 import threading
 2 import time
 3 
 4 
 5 def time_out(timer):
 6     '''
 7     一个规定函数执行时间的装饰器
 8     :param timer:
 9     :return:
10     '''
11 
12     def wrapper(func):
13         def __wrapper():
14             start_time = time.time()
15             # 通过设置守护线程强制规定函数的运行时间
16             t = threading.Thread(target=func)
17 
18             t.setDaemon(True)
19             t.start()
20             time.sleep(timer)
21             if t.is_alive():
22                 # 若在规定的运行时间未结束守护进程,则主动抛出异常
23                 raise Exception('Function execution timeout')
24             print (time.time()-start_time)
25 
26         return __wrapper
27 
28     return wrapper
29 
30 
31 @time_out(50)
32 def timer():
33     time.sleep(10)
34 
35 
36 timer()

 



posted @ 2020-01-17 17:55  情调丶  阅读(135)  评论(0编辑  收藏  举报