The main implementation of the membership information to increase, modify, query, and delete functions, writing to play,
In the process of writing, encountered problems, old new member information data merge, mobile phone number and mailbox verification,
#!/usr/bin/env python
# Coding=utf8
#author: [email protected]
Import os, sys, Time,json
Import re
Member_dict = \
{}
Member_name_list = []
Member_list = []
def handler_member_storage (name,passwd,mobile,email):
member_detailed = {}
member_detailed[' name '] = Name
member_detailed[' passwd '] = passwd
member_detailed[' Mobile ' = Mobile
member_detailed[' email ' = Email
Member_list.append (member_detailed)
Member_name_list.append (name)
member_dict[' NameList '] = member_name_list
member_dict[' Namedetail '] = member_list
#with Open (' Member_info.json ', ' W ') as F:
# Json.dump (member_dict,f)
Return member_dict
def Handel_query_user (x):
"""
Determine if x is str, query the name field, and if it is int, query the phone number field
:p Aram X:
: return:
"""
#print "string", isinstance (x,str)
#print "number", isinstance (x,int)
If Isinstance (x,str):
If x in Load_member_data () [' namelist ']:
Print "exact match query user [%s] information ..."% (x)
Print "==================================================="
User_index = Load_member_data () [' namelist '].index (x)
User_detail = Load_member_data () [' namedetail '][user_index]
For k,v in User_detail.items ():
Print K,v
Print "----------------------------------------------------"
Else
Print "\033[33;1m user name or password is not in this database, are you mistaken? you think again!! \033[0m "
If Isinstance (x,int):
If check_mobileormail_member (str (x))! = "mobile_true":
Print "this is the phone number, hit and try ..."
Else
Print "exact match query user phone [%s] information ..."% (x)
Print "===================================================="
User_detail = Load_member_data () [' namedetail ']
For user_dict in User_detail:
if x = = int (user_dict[' Mobile '):
For k,v in User_dict.items ():
Print K,v
Print "------------------------------------------------"
def Load_member_data ():
With open ("member_info.json", ' r ') as F:
Try
Member_info_dict = Json.load (f)
Return member_info_dict
Except Valueerror:
#print "\033[33;1m no data to load \033[0m"
Return False
def Check_login (user,passwd):
"""
Verify that the login user name and password are correct
:p Aram User:
:p Aram Passwd:
: return:
"""
With open ("member_info.json", ' r ') as F:
Member_info_dict = Json.load (f)
#print member_info_dict
If user in member_info_dict[' namelist ']:
User_index = member_info_dict[' NameList '].index (user)
User_detail = member_info_dict[' Namedetail '][user_index]
#print User_index,user_detail
if passwd = = user_detail[' passwd ' and user = = user_detail[' name ']:
Print "\033[32;1m input user [%s] password [%s] correct \033[0m"% (user,passwd)
Return True
Else
Print "\033[31;1m The password you entered is incorrect \033[0m"
Else
Print "\033[31;1m The user you are querying does not exist \033[0m"
def Check_mobileormail_member (x):
P=re.compile (r ' ^[\w\d]+[\d\w\_\.) [email protected] ([\d\w]+) \. ([\d\w]+) (?:\. [\d\w]+)? $|^ (?: \ +86)? (\d{3}) \d{8}$|^ (?: \ +86)? (0\d{2,3}) \d{7,8}$ ')
m = P.match (x)
if m = = None:
Print "\033[33;1mmail or mobile number is wrong!! \033[0m "
Return False
Else
If M.group (1)!=none:
If m.group (1) = = ' VIP ':
print ' It is%s mail,the address is%s '% (m.group (2), m.group ())
Return "mail_true"
Else
print ' It is%s mail,the address is%s '% (m.group (1), m.group ())
Return "mail_true"
Else
If M.group (3)!=none:
print ' It is mobilephone number,the number is%s '%m.group ()
Return "mobile_true"
Else
print ' It is telephone number,the number is%s '%m.group ()
Return True
Class Modify_user_info:
"""
Modify information about a user
"""
def UPDATE_PASSWD (self,user,newpwd):
If user in Load_member_data () [' namelist ']:
User_index = Load_member_data () [' namelist '].index (user)
User_detail = Load_member_data () [' namedetail '][user_index]
user_detail[' passwd '] = newpwd
Newpwd_dict = Load_member_data ()
newpwd_dict[' Namedetail '][user_index] = User_detail
Return newpwd_dict
Else
Print "are you mistaken!" The user you entered is not in the database "
def Update_mobile (self,user,newmobile):
If user in Load_member_data () [' namelist ']:
User_index = Load_member_data () [' namelist '].index (user)
User_detail = Load_member_data () [' namedetail '][user_index]
user_detail[' Mobile '] = Newmobile
Newpwd_dict = Load_member_data ()
newpwd_dict[' Namedetail '][user_index] = User_detail
Return newpwd_dict
Else
Print "are you mistaken!" The user you entered is not in the database "
def Update_email (self,user,newmail):
If user in Load_member_data () [' namelist ']:
User_index = Load_member_data () [' namelist '].index (user)
User_detail = Load_member_data () [' namedetail '][user_index]
user_detail[' email ' = NewMail
Newpwd_dict = Load_member_data ()
newpwd_dict[' Namedetail '][user_index] = User_detail
Return newpwd_dict
Else
Print "are you mistaken!" The user you entered is not in the database "
def Del_user_info (user):
If user in Load_member_data () [' namelist ']:
User_index = Load_member_data () [' namelist '].index (user)
User_detail = Load_member_data () [' namedetail '][user_index]
New_del_dict = Load_member_data ()
new_del_dict[' Namedetail '].remove (user_detail)
new_del_dict[' NameList '].remove (user)
Return new_del_dict
Else
Print "the user you want to delete [%s] is not in the Database"%user
if __name__ = = "__main__":
Operation_type = {
' 1 ': ' Add user information ',
' 1.1 ': ' Name,passwd,mobile,email ',
' 2 ': ' Query the user information ',
' 3 ': ' Modify the user information ',
' 4 ': ' Delete user information ',
' 5 ': ' logout '
}
While True:
Print "" "
\033[32;1m********************************************************************************
Welcome to login user information center, your can make the following operation:
1,%s
2,%s
3,%s
4,%s
5,%s
What do you need to operate?
\033[0m
"" "% (operation_type[' 1 '],operation_type[' 2 '],operation_type[' 3 '],operation_type[' 4 '],operation_type[' 5 '])
option = Raw_input ("Choice Is?>>")
if option = = ' 1 ':
Print "\033[32;1m you are choice is [%s]\033[0m"%operation_type[' 1 ']
Print "need to complete these input according to [%s] the guidelines"%operation_type[' 1.1 ']
While True:
Name = Raw_input ("name:")
passwd = Raw_input ("passwd:")
Mobile = Raw_input ("mobile:")
email = raw_input ("email:")
If len (name) = = 0 or len (passwd) = = 0 or len (mobile) = = 0 or len (email) = = 0:
Print "\033[33;1mthe user information you entered any item can ' t be empty or incorrect. Please fill in again!! \033[0m "
Continue
If check_mobileormail_member (mobile)! = "mobile_true" or check_mobileormail_member (email)! = "mail_true":
Continue
#print "######## #3", Name,passwd,mobile,email
Print "" "\033[32;1m You enter the following information:
name:%s
passwd:%s
mobile:%s
email:%s
\033[0m "" "% (name,passwd,mobile,email)
S1 = raw_input ("y save/w re-fill >>")
if S1 = = "y":
Old_user_dict = Load_member_data ()
New_user_dict = Handler_member_storage (name,passwd,mobile,email)
#print "########### #33", old_user_dict, ' \ n ', new_user_dict
#合并旧的和新的用户数据
if old_user_dict = = False:
Print "no old data can be loaded, write new data directly"
With open (' Member_info.json ', ' W ') as F:
Json.dump (new_user_dict,f)
S11 = Raw_input ("t continue to add/b to return to the upper level/q exit Operation?>>")
if S11 = = "t":
Continue
elif S11 = = "b":
Break
elif S11 = = "q":
Sys.exit (0)
Else
For user in new_user_dict[' namelist ']:
old_user_dict[' NameList '].append (user)
For User_detail in new_user_dict[' namedetail ']:
old_user_dict[' Namedetail '].append (user_detail)
#print old_user_dict,
With open (' Member_info.json ', ' W ') as F:
Json.dump (old_user_dict,f)
Print "saved successfully"
S12 = Raw_input ("t continue to add/b to return to the upper level/q exit Operation?>>")
If S12 = = "t":
Continue
Elif S12 = = "b":
Break
Elif S12 = = "q":
Sys.exit (0)
elif S1 = = "w":
Continue
Else
Pass
elif option = = ' 2 ':
Print "\033[32;1m you are choice is [%s]\033[0m"%operation_type[' 2 ']
Print "\033[33;1m friendly tip: Enter the correct user name and password to query the user information \033[0m"
Luser = raw_input ("user name:"). strip ()
LPASSWD = raw_input ("user password:"). strip ()
If check_login (luser,lpasswd) = = True:
Print "user authentication through"
Print "please Enter the user name or phone number that needs to be queried [currently only for exact queries]"
While True:
Query_info = raw_input ("query:>>"). strip ()
If check_mobileormail_member (query_info) = = "mobile_true":
Handel_query_user (int (query_info))
Else
Handel_query_user (query_info)
S2 = Raw_input ("b returns to the previous level/t continue Query")
If S2 = = "b":
Break
elif S2 = = "t":
Continue
Else
Print "user name or password is incorrect and cannot be queried for other Person's information"
elif option = = ' 3 ':
Print "\033[32;1m you are choice is [%s]\033[0m"%operation_type[' 3 ']
Print "friendly Reminder: currently support to modify the User's password, mobile phone number, Email address"
Modify = Modify_user_info ()
Modify_option = raw_input ("p change password//modify phone number//modify e-mail Address")
if modify_option = = "p":
user_name = Raw_input ("user name:"). strip ()
User_pwd = raw_input ("new password:"). strip ()
Print "start modifying user [%s] New password for [%s] ..."% (user_name,user_pwd)
Update_dict = modify.update_passwd (user_name,user_pwd)
With open (' Member_info.json ', ' W ') as F:
Json.dump (update_dict,f)
Print "password modified successfully!"
elif Modify_option = = "m":
user_name = Raw_input ("user name:"). strip ()
User_mobile = raw_input ("new phone number:"). strip ()
Print "start modifying user [%s] New phone number is [%s] ..."% (user_name,user_mobile)
Update_dict = Modify.update_mobile (user_name,user_mobile)
With open (' Member_info.json ', ' W ') as F:
Json.dump (update_dict,f)
Print "phone number modified successfully!"
elif Modify_option = = "e":
user_name = Raw_input ("user name:"). strip ()
User_mail = raw_input ("new mailbox:"). strip ()
Print "start modifying user [%s] new mailbox is [%s] ..."% (user_name,user_mail)
Update_dict = Modify.update_email (user_name,user_mail)
With open (' Member_info.json ', ' W ') as F:
Json.dump (update_dict,f)
Print "mailbox modified successfully!"
elif option = = ' 4 ':
Print "\033[32;1m you are choice is [%s]\033[0m"%operation_type[' 4 ']
Print "friendly reminder: Please enter your username"
user_name = Raw_input ("user Name:")
New_del_dict = Del_user_info (user_name)
With open (' Member_info.json ', ' W ') as F:
Json.dump (new_del_dict,f)
Print "user [%s] deleted successfully!!"% ( User_name)
elif option = = ' 5 ':
Print "\033[32;1m you are choice is [%s]\033[0m"%operation_type[' 5 ']
Sys.exit (0)
Else
Print "\033[31;1m The choice of the invalid \033[0m"
#print check_mobileormail_member (' 123456 ')
#print check_login (' testwqk ', ' testwqk ')
#print Handel_query_user (18600404875)
Python Learning member information additions and deletions change