Simple python background management program and python background management
I. Job Requirements
Ii. Flowchart
Iii. source code and specific ideas
Import shutilimport osimport sysUSER_LOGIN = {'is _ login': False} def outer (func): # decorator. If no login is made, output "Please log in" def inner (* args, ** kwargs): if USER_LOGIN ['is _ login']: r = func (* args, ** kwargs) return r else: print ("Please log in ") return innerdef outer1 (func): # decorator. If it is not an administrator, the output "insufficient Permissions" def inner1 (* args, ** kwargs ): if USER_LOGIN ['user _ type'] = '2': r = func (* args, ** kwargs) return r else: print ("not an administrator, not authorized ") return inner1 @ ou Terdef change_pwd (changed_user, changed_pwd, type_user): # change the password if type_user = '1': print ("Welcome % s to change the password ". center (50, '-') % USER_LOGIN ['current _ user']) with open ('regist', 'R', encoding = 'utf-8') as f1, open ('regist _ new', 'w', encoding = 'utf-8') as f2: for line in f1: read_list = line. strip (). split ('|') if read_list [0] = changed_user: read_list [1] = changed_pwd f2.write ('| '. join (read_list) + '\ n') conti Nue f2.write (line) shutil. move ('regist', 'regist _ Bak') # copy the regist file to the regist_bak file to go to the OS. rename ('regist _ new', 'regist') # rename regist_new to regist! Else: print ("You are not authorized to change the administrator password") @ outerdef look_information (): # view the current user information print ("Welcome % s to view information ". center (50, '-') % USER_LOGIN ['current _ user']) with open ('regist', 'r + ', encoding = 'utf-8 ') as f: for line in f: read_list = line. strip (). split ('|') if read_list [0] = USER_LOGIN ['current _ user']: print ("username: % s" % read_list [0]) print ("Password: % s" % read_list [1]) print ("Email: % s" % read_list [2]) print ("phone: % s "% read_list [3]) def login (user, pwd): # login with open ('regist', 'r + ', encoding = 'utf-8 ') as f: for line in f: read_list = line. strip (). split ('|') if read_list [0] = user and read_list [1] = pwd: # if the user has USER_LOGIN ['is _ login'] = True USER_LOGIN ['current _ user'] = user USER_LOGIN ['user _ type'] = read_list [-1] print ("Welcome % s login ". center (50, '-') % USER_LOGIN ['current _ user']) break if not USER_LOGIN ['is _ login']: # if the user has not registered print ("the user does not exist, register") def register (reg_user, reg_pwd, reg_email, reg_phone): # register register_list = [] register_list.append (reg_user) # Add User information to the list register_list.append (reg_pwd) register_list.append (reg_email) register_list.append (reg_phone) login ('1') with open ('regist', 'A ', encoding = 'utf-8') as f: # write user information to the file li = '| '. join (register_list) f. write (li + '\ n') print ("Register/Add information:", li) def delete_func (dele_user, type_user ): # delete a common user if type_user = '1': # if a common user exit_flag = False with open ('regist', 'r + ', encoding = 'utf-8') as f1, open ('regist _ new', 'w', encoding = 'utf-8') as f2: for line in f1: ret_list = line. strip (). split ('|') if ret_list [0] = dele_user: exit_flag = True # The flag is originally set to False. When a user is found to be deleted, flag is set to True print ("normal user deleted successfully") continue f2.write (line) shutil. move ('regist', 'regist _ Bak') OS. rename ('regist _ new', 'regist') if not exit_flag: print ("normal user to be deleted does not exist") elif type_user = '2 ': # If the deleted administrator user print ("no permission to delete the Administrator account") def upper_level (upper_user): # Upgrade to administrator with open ('regist', 'r + ', encoding = 'utf-8') as f1, open ('regist _ new', 'w', encoding = 'utf-8') as f2: for line in f1: li = line. strip (). split ('|') if li [0] = upper_user: li [-1] = '2' print ("% s has become Administrator" % upper_user) f2.write ('| '. join (li) + '\ n') continue f2.write (line) shutil. move ('regist', 'regist _ Bak') OS. rename ('regist _ new', 'regist') def search (search_info): # simple search with open ('regist', 'r + ', encoding = 'utf-8') as f: for line in f: ret_list = line. strip (). split ('|') if search_info in ret_list: print (ret_list) def get_usertype (user): # obtain the user type by using the local user name. 1 or 2 with open ('regist ', 'r + ', encoding = 'utf-8') as f: for line in f: li = line. strip (). split ('|') if li [0] = user: return li [-1] # return Account type 1 or 2 @ outer1def admin_user (): # The Administrator calls num = input ("select 1. change the password; 2. view the user information. 3. change the password of a common user. "" 4. delete/Add a common user; 5. permission management; 6. keyword: search common user information; 7. exit: ") if num = '1': new_pwd = input (" enter a new password: ") change_pwd (new_pwd) elif num = '2': look_information () elif num = '3': user_changed = input ("Enter the username to change the password:") type_user = get_usertype (user_changed) new_pwd = input ("Enter the new password: ") change_pwd (user_changed, new_pwd, type_user) elif num = '4': add_or_dele = input (" 1. delete a common user. 2. add common user ") if add_or_dele = '1': delete_username = input (" Enter the username of the common user to be deleted: ") type_user = get_usertype (delete_username) delete_func (delete_username, type_user) elif add_or_dele = '2': regi_username = input ("Enter the registration Username:") regi_pwd = input ("Enter the registration password :") regi_email = input ("Enter your email:") regi_phone = input ("Enter your phone number:") register (regi_username, regi_pwd, regi_email, regi_phone) elif num = '5 ': upper_user = input ("Enter the normal user name to upgrade to the Administrator:") upper_level (upper_user) elif num = '6': search_information = input ("Enter the keyword to search: ") search (search_information) elif num = '7': sys. exit () def main (): while True: choice = input ("select: 1. login; 2. registration; 3. change the password. 4. view information; 5. background management; 6. exit: ") if choice = '1': username = input (" Enter the username: ") password = input (" enter the password: ") login (username, password) elif choice = '2': regi_username = input ("Enter the registration Username:") regi_pwd = input ("Enter the registration password :") regi_email = input ("Enter your email:") regi_phone = input ("Enter your phone number:") register (regi_username, regi_pwd, regi_email, regi_phone) print ("registered successfully ". center (50, '*') elif choice = '3': new_pwd = input ("enter a new password :") current_user = USER_LOGIN ['current _ user'] change_pwd (current_user, new_pwd) print ("Password Changed successfully, please log on again ". center (50, '-') sys. exit () elif choice = '4': look_information () elif choice = '5': admin_user () elif choice = '6': sys. exit () main ()
For convenience, paste the code without folding
@ Outer1def admin_user (): # The Administrator calls num = input ("select 1. change the password; 2. view the user information. 3. change the password of a common user. "" 4. delete/Add a common user; 5. permission management; 6. keyword: search common user information; 7. exit: ") if num = '1': new_pwd = input (" enter a new password: ") change_pwd (new_pwd) elif num = '2': look_information () elif num = '3': user_changed = input ("Enter the username to change the password:") type_user = get_usertype (user_changed) new_pwd = input ("Enter the new password: ") change_pwd (user_changed, new_pwd, type_user) elif num = '4': add_or_dele = input (" 1. delete a common user. 2. add common user ") if add_or_dele = '1': delete_username = input (" Enter the username of the common user to be deleted: ") type_user = get_usertype (delete_username) delete_func (delete_username, type_user) elif add_or_dele = '2': regi_username = input ("Enter the registration Username:") regi_pwd = input ("Enter the registration password :") regi_email = input ("Enter your email:") regi_phone = input ("Enter your phone number:") register (regi_username, regi_pwd, regi_email, regi_phone) elif num = '5 ': upper_user = input ("Enter the normal user name to upgrade to the Administrator:") upper_level (upper_user) elif num = '6': search_information = input ("Enter the keyword to search: ") search (search_information) elif num = '7': sys. exit () def main (): while True: choice = input ("select: 1. login; 2. registration; 3. change the password. 4. view information; 5. background management; 6. exit: ") if choice = '1': username = input (" Enter the username: ") password = input (" enter the password: ") login (username, password) elif choice = '2': regi_username = input ("Enter the registration Username:") regi_pwd = input ("Enter the registration password :") regi_email = input ("Enter your email:") regi_phone = input ("Enter your phone number:") register (regi_username, regi_pwd, regi_email, regi_phone) print ("registered successfully ". center (50, '*') elif choice = '3': new_pwd = input ("enter a new password :") current_user = USER_LOGIN ['current _ user'] change_pwd (current_user, new_pwd) print ("Password Changed successfully, please log on again ". center (50, '-') sys. exit () elif choice = '4': look_information () elif choice = '5': admin_user () elif choice = '6': sys. exit () main ()
Iv. Summary
1. How to change the password?
I was blinded. Because f. write (xx) is directly written at the end of the file. In this case, I want to write it at the end.
However, how can I delete the original password change record on the last line? A little more trouble.
In other words, what should I do if I change that line without adding it to the last line?
Shutil. move ('regist', 'regist _ Bak') # copy the regist file to the regist_bak file to go to the OS. rename ('regist _ new', 'regist') # rename regist_new to regist!
2. How to modify user user_type (Change 1 to 2)
I have seen some blogs with the replace () method.
New_str = line. replace (read_list [1], changed_pwd) I used it at the beginning, but there was a BUG later!
For example, when the user name is the same as the password, the password is also changed. Besides, the replace () method is used to modify
User_type also changes the user name. At that time, I again called monbi. This is not scientific!
Later, I checked the source code of replace () to find that I was wrong.
You can also look at the Python replace () method
3. After I log on to the Administrator and add a common user, I find that I cannot log on to the administrator again.
So I went back to check register (reg_user, reg_pwd, reg_email, reg_phone ),
I found that I abused global variables
USER_LOGIN['user_type'] = '1'register_list.append(USER_LOGIN['user_type'])
In the past, I assigned USER_LOGIN ['user _ type'] = '1' again'
To solve this problem, I wrote the get_user_type () method.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.