Create a python background management program

Source: Internet
Author: User
This article describes in detail how to implement a simple python background management program, which has some reference value, interested friends can refer to this article for details about the implementation method of the simple python background management program, which has some reference value. interested friends can refer

I. job requirements

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 a simple python background management program details, please pay attention to other articles in the first PHP community!

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.