python学习之-成员信息增删改查
主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,
在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,
#!/usr/bin/env python
# coding=utf8
#author:shantuwqk@163.com
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):
"""
判断x如果是str,查询name字段,如果是int,查询手机号字段
:param x:
:return:
"""
#print "字符串",isinstance(x,str)
#print "数字",isinstance(x,int)
if isinstance(x,str):
if x in load_member_data()[‘namelist‘]:
print "精确匹配查询用户[%s]信息....."%(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 " 33[33;1m用户名或密码不在本数据库,你是不是记错了?您再想想!! 33[0m"
if isinstance(x,int):
if check_mobileORmail_member(str(x)) != "mobile_true":
print "这是手机号吗,打打试试...."
else:
print "精确匹配查询用户手机[%s]信息......"%(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 " 33[33;1m没有数据可加载 33[0m"
return False
def check_login(user,passwd):
"""
验证登录用户名和密码是否正确
:param user:
:param 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 " 33[32;1m输入用户[%s]密码[%s]正确 33[0m"%(user,passwd)
return True
else:
print " 33[31;1m 您输入的密码不正确 33[0m"
else:
print " 33[31;1m你查询的用户不存在 33[0m"
def check_mobileORmail_member(x):
p=re.compile(r‘^[wd]+[dw\_.]+@([dw]+).([dw]+)(?:.[dw]+)?$|^(?:+86)?(d{3})d{8}$|^(?:+86)?(0d{2,3})d{7,8}$‘)
m = p.match(x)
if m == None:
print " 33[33;1mmail or mobile number is wrong!! 33[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:
"""
修改用户的相关信息
"""
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 "你有没有搞错!你输入的用户不在数据库"
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 "你有没有搞错!你输入的用户不在数据库"
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 "你有没有搞错!你输入的用户不在数据库"
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 "你要删除的用户[%s]不在数据库里"%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 """