Decorator job

 1. Write decorator, multiple authentication functions plus functions (from the user's account password file) require a login is successful, the follow-up functions are no longer need to enter a user name and password

. 1 DL = False # define a benchmark for determining the condition
 2  DEF Login (FUNC):
 . 3      DEF Inner (* args, ** kwargs):
 . 4          Global DL # globals
 . 5          IF DL: if true # executed
 . 6              the rel = FUNC ()
 . 7              return the rel
 . 8          the else :
 . 9              username = iNPUT ( " Please enter your name: " )
 10              password = iNPUT ( " enter password: " )
 . 11              IF username == " wzc27229 " and password == "123456 " :
 12 is                  DL = True
 13 is                  the rel = FUNC ()
 14                  return the rel
 15              the else :
 16                  Print ( " input error " )
 . 17      return Inner
 18 is  
. 19  @login
 20 is DEF add_goods (* args, ** kwargs):
 21 is      Print ( " add items " )
 22  
23  @login
 24- DEF del_goods (* args, ** kwargs):
 25      Print ( "Remove goods " )
 26  
27  add_goods ()
 28 del_goods ()
View Code

 2. Write decorator, plus multiple function calls recording function, requires each function call function name will be written to a file called

. 1  DEF wapper (FUNC):
 2      DEF Inner (* args, ** kwargs):
 . 3          with Open ( ' log ' , ' A ' , encoding = ' UTF-. 8 ' ) AS F: # File Open log, added to manner
 . 4              f.write (FUNC .__ name __ + ' \ n- ' ) # Get the name of the function
 . 5          File = FUNC ()
 . 6          return File
 . 7      return Inner
 . 8  
. 9  @wapper
 10  DEF add_goods ():
 . 11      Print ( "Add product " )
 12 is  
13 is  @wapper
 14  DEF del_goods ():
 15      Print ( " remove items " )
 16  
. 17  add_goods ()
 18 is  add_goods ()
 . 19 add_goods ()

 

Guess you like

Origin www.cnblogs.com/wzc27229/p/10993915.html