Basic Python-decorator and basic python Decoration

Source: Internet
Author: User

Basic Python-decorator and basic python Decoration

To be updated:

 

 

 

Decorator instance

Program requirements:

  • You can log on up to three times
  • When an existing user fails to log on three times, the user is locked to restrict login.

Program code:

1 import time 2 3 4 def run_timer (func): # timer function 5 def wrapper (* args, ** kwargs): # decorator 6 start = time. time () # start timing 7 func (* args, ** kwargs) # run the program 8 end = time. time () # Stop timing 9 print ('run time is % ss' % (end-start) # print program running duration 10 11 return wrapper12 13 14 def identiy (): 15 "user login verification program 16 you can try three logon times at most 17 when an existing user enters the wrong password three times, lock the user, restrict login to "" 18 with open ('account _ Bak', 'r + ') as f_account, open ('locked _ list', 'a +') F_locked: 19 f = 0 # variable returned by the program 20 l = [] # Locked User List 21 user_input = [] # username list with incorrect password entered 22 count = 0 # logon count counter 23 flag = True #24 while flag and count login cycle control switch <3:25 name = input ('Please input username: ') # enter the user name 26 pwd = input ('Please input password:') # enter the User password 27 f_locked.seek (0) # after the "a +" mode is enabled, the file location is at the end, to traverse the file content, you need to move the pointer to the start position of the file 28 for locked_user in f_locked: # Write the Locked User name to the List 29 l. append (locked_user.strip () 30 if name in L: 31 print ('this user has been locked! ') # If the current login user is in the locked list, prompt and re-enter the login information 32 else: 33 user_input.append (name) # Add the currently entered user name to the List 34 f_account.seek (0) # Move the file location to the starting position 35 for line in f_account before the loop: # traverse user login data file 36 s = eval (line) # convert the content of the file to the dictionary format 37 if name = s ['name'] and pwd = s ['Password']: # determining whether the user name and password are correct 38 print ('authenticate successful ') # matching the user name and password, successful authentication, end cycle, then, return f = 1 39 f = 140 flag = False41 break42 if f = 1: # the user name and password do not match, prompting the user to enter the Error 43 continue44 print ('wr Ong name or password! ') 45 count + = 1 # increase the number of errors by 1. When count is equal to 3, the end loop 46 if len (user_input) = 3: # if the list length is equal to 3, the user fails to log on three times. 47 if user_input [0] = user_input [1] = user_input [2]: # determine whether the same user name is 48 f_account.seek (0) # reset the file location to the starting position 49 l = [] # create an empty list and store the username information in the user login file 50 for line in f_account: # traverse user login file 51 s = eval (line) # convert row content to dictionary format 52 l. append (s ['name']) # Add the user name to list 53 if user_input [0] in l: # determine whether the user name that fails to log on is 54 print ('this user has been lock in the above list Ed! ') # Prompt that the user will lock the login name 55 f_locked.write (user_input [0] +' \ n') # Add the login name to the lock file 56 f_locked.flush () # Real-Time refresh file 57 return f58 59 60 def auth (source): 61 "decorator of the user login authentication program" 62 def auth_main (func ): 63 def wrapper (* args, ** kwargs): 64 if source = 'file': # determine whether the authentication source type is 'file' 65 if (identiy () = 1): # Call the user login authentication program 66 res = func (* args, ** kwargs) # Run the decorated Program 67 return res68 elif source = 'ldap ': # Another authentication source type 69 def wrapper_2 (* args, ** k Wargs): 70 print ('Nothing here! ') 71 pass72 return wrapper73 return auth_main74 75 76 @ auth (source = 'file ') # describe the user login authentication function decoration program 'func _ 1' 77 @ run_timer # describe the program timing function decoration program 'func _ 1' 78 def func_1 (): # decoration program 79 time. sleep (1) 80 print ('this is function_1 ') 81 82 83 func_1 () # Call the decored Program
Timing + login certification decorator

Verification process:

1. account file: stores user login data

2. locked_list file: stores the Locked User Name

Currently empty

3. Try to log on to the correct user

4. Failed to log on to different users

5. Failed to log on to the same user

6. Failed to log on to the same user, but the user does not exist.

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.