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 "\033[33;1m用户名或密码不在本数据库,你是不是记错了?您再想想!!\033[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 "\033[33;1m没有数据可加载\033[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 "\033[32;1m输入用户[%s]密码[%s]正确\033[0m"%(user,passwd)
return True
else:
print "\033[31;1m 您输入的密码不正确\033[0m"
else:
print "\033[31;1m你查询的用户不存在\033[0m" def check_mobileORmail_member(x):
p=re.compile(r'^[\w\d]+[\d\w\_\.]+@([\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:
"""
修改用户的相关信息
"""
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 """
\033[32;1m********************************************************************************
Welcome to login user information center, you can make the following operation:
1,%s
2,%s
3,%s
4,%s
5,%s
What 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 have choice is [%s]\033[0m"%Operation_type['1']
print "You 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 您输入的信息如下:
name:%s
passwd:%s
mobile:%s
email:%s
\033[0m"""%(name,passwd,mobile,email)
s1 = raw_input("y 保存/w 重新填写>>")
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 "没有旧数据可加载,直接写入新数据"
with open('member_info.json','w') as f:
json.dump(new_user_dict,f)
s11 = raw_input("t 继续添加/b 返回上一级/q退出操作?>>")
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 "保存成功"
s12 = raw_input("t 继续添加/b 返回上一级/q退出操作?>>")
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 have choice is [%s]\033[0m"%Operation_type['2']
print "\033[33;1m友情提示:输入正确的用户名和密码才能查询用户信息\033[0m"
luser = raw_input("用户名:").strip()
lpasswd = raw_input("用户密码:").strip()
if check_login(luser,lpasswd) == True:
print "用户验证通过"
print "请输入需要查询的用户名或手机号[目前只支持精确查询]"
while True:
query_info = raw_input("查询:>>").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 返回上一级/t 继续查询")
if s2 == "b":
break
elif s2 == "t":
continue else:
print "用户名或密码不正确,不能查询其它人信息" elif option == '3':
print "\033[32;1m you have choice is [%s]\033[0m"%Operation_type['3']
print "友情提示:目前支持修改用户的密码,手机号,邮箱地址"
modify = Modify_User_Info()
modify_option = raw_input("p 修改密码/m 修改手机号/e 修改邮箱地址")
if modify_option == "p":
user_name = raw_input("用户名:").strip()
user_pwd = raw_input("新密码:").strip()
print "开始修改用户[%s]新的密码为[%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 "密码修改成功!"
elif modify_option == "m":
user_name = raw_input("用户名:").strip()
user_mobile = raw_input("新手机号:").strip()
print "开始修改用户[%s]新的手机号为[%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 "手机号修改成功!"
elif modify_option == "e":
user_name = raw_input("用户名:").strip()
user_mail = raw_input("新邮箱:").strip()
print "开始修改用户[%s]新的邮箱为[%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 "邮箱修改成功!" elif option == '4':
print "\033[32;1m you have choice is [%s]\033[0m"%Operation_type['4']
print "友情提示:请输入用户名"
user_name = raw_input("用户名:")
new_del_dict = del_user_info(user_name)
with open('member_info.json','w') as f:
json.dump(new_del_dict,f)
print "用户[%s]删除成功!!"%(user_name)
elif option == '5':
print "\033[32;1m you have 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学习之成员信息增删改查的更多相关文章
- python学习之-成员信息增删改查
python学习之-成员信息增删改查 主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env p ...
- python 学习分享-实战篇增删改查作业
一大波函数来袭 作业要求: 1本次作业通过空格及逗号,将文件拆分成列表,在通过判断add.del.update.select等关键字,来判断用户执行的是哪种命令,根据不同的命令调用不同的函数去处理. ...
- Python学习笔记-列表的增删改查
- python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)
python全栈开发中级班全程笔记 第三章:员工信息增删改查作业代码 作业要求: 员工增删改查表用代码实现一个简单的员工信息增删改查表需求: 1.支持模糊查询,(1.find name ,age fo ...
- EF学习笔记-1 EF增删改查
首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...
- python manage.py shell 的增删改查
python manage.py shell 的增删改查 guoguo-MacBook-Pro:myblog guoguo$ python manage.py shell Python 3.5.1 ( ...
- python基础学习之类的属性 增删改查
类中的属性如何在类外部使用代码进行增删改查呢 增加.改变: setattr内置函数以及 __setattr__魔法方法 class A: aaa = '疏楼龙宿' a = A() setattr(a, ...
- python 10min系列之实现增删改查系统
woniu-cmdb 奇技淫巧--写配置文件生成增删改查系统 视频教程 项目主页跪求github给个star, 线上demo,此页面都是一个配置文件自动生成的 详细的文章介绍和实现原理分析会发布在我的 ...
- 记一些Python(Pymysql)建表、增删改查等基础操作(小白适用)
1.读取sql文件创建数据表 有一个形如下图的sql文件,使用python读取文件并在数据库中创建所有的表. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道 ...
随机推荐
- C语言学习之笔记
第一章 概述 1. C语言的特点 ①语言简洁.紧凑,使用方便.灵活.共有32个关键字(也称保留字),9种控制语句. ②运算符丰富,共有34种运算符. ③数据结构丰富,数据类型有:整型.实型.字符型.数 ...
- hibernate 一张数据表的流程
1. 写一个domain类来映射数据库表 2. 写一个*.hbm.xml文件来配置映射 <?xml version="1.0"?> <!DOCTYPE hiber ...
- Unity NGUI 网络斗地主 -制作图集 Atlas
Unity NGUI 网络斗地主 -制作图集 Atlas by @杨海龙 开发环境 Win7+Unity4.2.1f4+NGUI 3.0.4版本 这一节告诉大家如何制作(图集)Atlas! 1.首 ...
- Color the Fence
Codeforces Round #202 (Div. 2) B:http://codeforces.com/problemset/problem/349/B 题意:给你一些颜料,然后你可以用这些颜料 ...
- Catch That Cow
poj3278:http://poj.org/problem?id=3278 题意:给你一个n和k,n可以加1也可以减1,还可以乘2,现在要求n经过这样的几步变换可以使得n==k:求得最小的步数.题解 ...
- 使用 getNextException() 来检索已经过批处理的特定元素的异常。 ERRORCODE=-4228, SQLSTATE=null
今天查询了一天发现的问题,用ibatis做批量操作时,报错: [非原子批处理出现故障]使用 getNextException() 来检索已经过批处理的特定元素的异常. ERRORCODE=-4228, ...
- 除了创建时指定窗口位置之外,还有3种移动窗口位置的办法(移动的同时往往可以改变窗口大小)(SetWindowPos最有用,它有许多标志位)
首先,在创立窗口对象的时候,CreateWindowEx就可以指定窗口的位置.除此之外,还有三种方法可以改变窗口的位置: procedure TWinControl.CreateWindowHandl ...
- PlatformTransactionManager
Spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactio ...
- Samples DataBind FastJson循环引用问题
Fastjson full support databind, it's simple to use. Encode import com.alibaba.fastjson.JSON; Group g ...
- Church encoding
In mathematics, Church encoding is a means of representing data and operators in the lambda calculus ...