Python Learning member information additions and deletions change

Source: Internet
Author: User

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

Related Article

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.