Python Note 4 (Advanced Decorator)

advanced needs

1. The first case

You need to add decorators to 500 functions. If you add or remove them one by one, it will be very troublesome and wasteful;
now, you can design your decorator to confirm whether it works.

1  import time
 2 FLAG = True #Global variable 
3  def outer(flag):
 4      def timmer(f):
 5          def inner(*args,** kwargs):
 6              if flag == True: #If flag is True, the original Some functions add functions before and after without changing the calling method of the original function 
7                  start_time = time.time()
 8                  ret = f(*args,** kwargs)
 9                  end_time = time.time()
 10                  print (end_time - start_time)
 11              else : #If flag is not True, only the original function is executed, and the calling method of the original function is not changed 
12                  ret = f(*args, ** kwargs)
 13              return ret
 14          return inner
 15      return timmer
 16  
17 @outer(FLAG)    # func = timmer(func) 
18  def func(a,b):
 19      print ( ' begin func ' ,a)
 20      time.sleep(0.1 )
 21      print ( ' end func ' ,b)
 22      return True
 23  
24 func(1 ,2)
 25  
26 #Execution  result 27 # >>>begin func 1 28 # >>>end func 2 29 # >>> 0.10027241706848145
 
 
 

2. The second case

In addition to calculating the execution time of the function, the decorator can also record logins, log records, etc.
Now if you want to access the blog garden home page or other premise, you need to log in before you can access, and design a decorator.

 1 import time
 2 login_info = {'alex':False}
 3 def login(func):   # manager
 4     def inner(name):
 5         if login_info[name] != True:
 6             user = input('user :')
 7             pwd = input('pwd :')
 8             if user == 'alex' and pwd == 'alex3714':
 9                 login_info[name] = True
 10          if login_info[name] == True:
 11              ret = func(name)      # inner 
12 in timmer              return ret
 13      return inner
 14  
15  @login
 16  def index(name):
 17      print ( ' Welcome %s came to the blog garden homepage~ ' % name)
 18  
19  @login
 20  def manager(name):
 21      print ( ' Welcome %s to the blog garden management page~ ' % name)
 22 
23 index( ' alex ' ) #Because login_info[('alex']==Flase, so the first access needs to log in 
24 index( ' alex ' )
 25 manager( ' alex ' )
 26 manager( ' alex ' )
 27  
28 #Execution  result 29 # >>>user: alex 30 # >>>pwd: alex3714 31 # >>>Welcome alex to the homepage of the blog garden~ 32 # >>>Welcome alex to the homepage of the blog garden ~ 33 # >>> Welcome alex to the blog garden management page~ 34 #
 
 
 
 
 
 >>>Welcome alex to the blog garden management page~

3. The third situation

 

1) Because two decorators are used, let's explain the execution process of two decorators decorating a function before we start:

 1 def wrapper1(func): 
 2     def inner1():
 3         print('wrapper1 ,before func')
 4         func() 
 5         print('wrapper1 ,after func')
 6     return inner1
 7 
 8 def wrapper2(func): 
 9     def inner2():
10         print('wrapper2 ,before func')
11         func() 
12         print('wrapper2 ,after func')
13     return inner2
14 
15 @wrapper2 
16 @wrapper1 
17 def f():
18     print('in f')
19 
20 f()

2) On the basis of the second case, the execution time of the function index and manger is also calculated:

 1 import time
 2 login_info = {'alex':False}
 3 def login(func):   # manager
 4     def inner(name):
 5         if login_info[name] != True:
 6             user = input('user :')
 7             pwd = input('pwd :')
 8             if user == 'alex' and pwd == 'alex3714':
 9                 login_info[name] = True
 10          if login_info[name] == True:
 11              ret = func(name)      # inner 
12 in timmer              return ret
 13      return inner
 14  
15  def timmer(f):
 16      def inner(*args,* * kwargs):
 17          start_time = time.time()
 18          ret = f(*args,**kwargs)      #call the decorated method 
19          end_time = time.time()       #
 20          print (end_time - start_time)
21          return ret
 22      return inner
 23  
24  @login
 25  @timmer
 26  def index(name):
 27      print ( ' Welcome %s to the homepage of Blog Park~ ' % name)
 28  
29  @login
 30 @timmer     # manager = login(manager ) 
31  def manager(name):
 32      print ( ' Welcome %s to the blog garden management page~ ' % name)
 33  
34 index( ' alex ' )
 35 index('alex')
36 manager('alex')
37 manager('alex')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936756&siteId=291194637