文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Python地址薄程序

Python地址薄程序

时间:2010-07-30  来源:kimkcn

也就是learning python最后一章作者所提的那个地址薄程序

这是我写的第一个python程序,由于之前没学过编程,所以过程非常曲折

里面的实现臃肿,废话连篇自然不用提了,不过总算也是辛苦了很多天

 

我这里比作者提的多实现了一个用户登录的功能

#coding:utf-8
import os
import sys
import md5
import getpass
import cPickle as p

INIT = '''地址薄程序,可用来记录任意用户信息,作者:闪马,时间:2010-7-14。
请注意按屏幕提示进行操作。
'''
LOGIN = '''您可以进行以下操作:
【f】:查找指定用户信息。 【a】:增加一个用户。
【d】:删除指定用户信息。 【l】:显示所有用户信息。
【m】:修改指定用户信息。 【e】:清空地址薄所有信息。
【h】:查看本帮助信息。 【s】:将地址薄所有信息写入文件,持续保存。
【o】:注销用户登陆。 【q】:退出地址薄程序。
注意:此程序操作并非即时存盘,所以请注意操作完成后存盘!
'''
NOLOGIN='''您目前还没有登陆,现在可以使用下面几项操作:
【1】:使用用户名登陆。
【2】:注册一个用户。
【0】:退出程序。
'''

print INIT

#Define func()
class address:
    def __init__(self): #初始化
        self.userDB='/root/login.db'
        if os.path.isfile(self.userDB)==False: #判断用户文件是否存在
            f=open(self.userDB,'w')
            p.dump('',f)
            f.close()
            self.user_dict = {}
        try:
            F=open(self.userDB)
            self.user_read_dict=p.load(F)
            self.user_dict=p.load(F)
            F.close()
            if not self.user_dict:
                self.user_dict={}
                self.user_dict_self = {}
        except EOFError: #文件存在,但内容为空的异常处理
            self.user_dict = {}
            self.user_read_dict = {}

    def check_equal(self): #判断读入数据和文件内容是否一致
        if self.addr_read_dict == self.addr_dict:
            return 'true'
        else:
            return 'false'

    def check_login(self): #判断是否登录
        if not self.user_read_dict:
            return 'false'
        for name in self.user_read_dict:
            if 'status' not in self.user_read_dict[self.name]:
                return 'false'
            if self.user_read_dict[self.name]['status']==0:
                return 'true'
            else:
                return 'false'

##未注册用户函数
    def login(self):
        try:
            f=open(self.userDB)
            self.user_read_dict=p.load(f)
            f.close()
        except EOFError:
            self.user_read_dict={}
        self.name=raw_input('请输入登录用户名:')
        if not self.name:
            return

#获取登录密码,并利用md5加密,然后将加密后的字符串和文件内的md5字符串进行比较
        get_pass=getpass.getpass('请输入密码:')
        if self.name not in self.user_read_dict:
            print '用户名或密码错误!'
            return
        passwd=md5.new(get_pass).hexdigest()
        if self.user_read_dict[self.name]['passwd']==passwd:
            self.user_read_dict[self.name]['status']=0
            os.system('clear')
            print '%s登录成功!'%self.name,
            self.menu()
        else:
            print '用户名或密码错误!'
            return

    def reg(self):
        try:
            f=open(self.userDB)
            user_dict=p.load(f)
            f.close()
        except EOFError:
            user_dict={}
        name=raw_input('请输入想要注册的用户名:')
        if not name:
            print '用户名不可为空!'
            return
        elif name in user_dict.keys():
            print '%s 用户已存在,请重新选择!'%name
            return
        while True:
            passwd1=getpass.getpass('请输入您要设置的密码:')
            if not passwd1:
                print '密码不可为空,退出!'
                return
            passwd2=getpass.getpass('请再次输入密码:')
            if not passwd1==passwd2:
                print '两次输入的密码不一致!'
                continue
        ##将得到的密码进行md5加密,并将加密后的字符串写入文件
            passwd=md5.new(passwd2).hexdigest()
            user_dict_self={'passwd':passwd,'status':1}
            user_dict[name]=user_dict_self
            f=open('/root/login.db','w')
            p.dump(user_dict,f)
            f.close()
            print '注册成功!'
            break


##注册用户函数
    def logout(self):
        for name in self.user_read_dict:
            if self.user_read_dict[name]['status']==0:
                self.user_read_dict[name]['status']=1
                os.system('clear')
                print '%s 已正常退出!' % self.name
                self.menu()

    def add(self):
        while True:
            name=raw_input('请输入用户名:')
            if not name:
                print '用户名称不可为空,退出!'
                return
            break
        self.addr_dict[name]={}
        addr_dict_self=self.addr_dict[name]
        while True:
            age=raw_input('请输入年龄:')
            tel=raw_input('请输入联系方式:')
            address=raw_input('请输入联系地址:')
            addr_dict_self['age']=age
            addr_dict_self['tel']=tel
            addr_dict_self['address']=address
            self.addr_dict[name]=addr_dict_self
            print '增加 %s 用户信息成功!' %name
            return

    def listall(self):
        if not self.addr_dict:
            print '当前地址薄内容为空!'
            return
        else:
            for name in self.addr_dict:
                print '姓名:%s' %name
                dict_self=self.addr_dict[name]
                print '年龄 :%s\n联系方式: %s\n地址: %s\n' %(dict_self['age'],dict_self['tel'],dict_self['address'])

    def search(self):
        while True:
            name=raw_input('输入想要查找的用户名称:')
            if not name:
                return
            if name in self.addr_dict.keys():
                print '姓名:%s' %name
                dict_self = self.addr_dict[name]
                print '年龄 :%s\n联系方式: %s\n地址: %s\n' %(dict_self['age'],dict_self['tel'],dict_self['address'])
            else:
                print '没有 %s 这个用户的相关信息。' %name
            break

    def delete(self):
        while True:
            name=raw_input('输入想要删除的用户名称:')
            if not name:
                return
            if name in self.addr_dict.keys():
                print '删除 %s 用户信息成功 !!' %name
                del self.addr_dict[name]
            else:
                print '没有 %s 这个用户的相关信息。' %name
            break

    def modify(self):
        name=raw_input('输入想要修改信息的用户名称:')
        if not name:
            return
        if name in self.addr_dict.keys():
            addr_dict_self=self.addr_dict[name]
            age=raw_input('请输入新的年龄:')
            tel=raw_input('请输入新的联系方式:')
            address=raw_input('请输入新的联系地址:')
            addr_dict_self['age']=age
            addr_dict_self['tel']=tel
            addr_dict_self['address']=address
            self.addr_dict[name]=addr_dict_self
            print '修改 %s 用户信息成功!更改后 %s 用户的信息是:' %(name,name)
            print '姓名:%s' %name
            print '年龄 :%s\n联系方式: %s\n地址: %s\n' %(addr_dict_self['age'],addr_dict_self['tel'],addr_dict_self['address'])

    def empty(self):
        if not self.addr_dict:
            print '当前地址薄内容为空,无法清空!'
        else:
            INPUT=raw_input('当前地址薄有内容,这个操作将清空地址薄中的所有信息,是否清空?[y/n]:')
            if INPUT=='y':
                self.addr_dict={}
                print '清空地址薄中所有用户信息成功!'
            elif INPUT=='n':
                pass

    def save(self):
        if self.check_equal() == 'false':
            INPUT=raw_input('写入操作将覆盖原有文件内容,此举不可恢复,确定覆盖么[y/n]?')
            if INPUT=='y':
                f=file(self.addrDB,'w')
                p.dump(self.addr_dict,f)
                f.close()
                print '成功将数据写入 %s!' %self.addrDB
                f=open(self.addrDB)
                self.addr_read_dict = p.load(f)
                f.close()
            else:
                return
        else:
            print '地址薄信息未作修改,无需保存!'


    def quit(self): #退出函数
        if self.check_login() == 'false':
            print '成功退出!'
            sys.exit()
        elif self.check_equal() == 'false':
            INPUT=raw_input('地址薄信息未保存,是否确认退出?[y/n]:')
            if INPUT=='y':
                print '成功退出!'
                sys.exit()
            else:
                pass
        else:
            print '成功退出!'
            sys.exit()


##main programing
    def menu(self): #用户菜单函数
        result=self.check_login() #判断用户的登录状态,并显示对应的菜单

##未登录用户
        if result=='false':
            print NOLOGIN
            while True:
                try:
                    INPUT=raw_input('>')
                    if not INPUT:pass
                    elif INPUT=='1':self.login()
                    elif INPUT=='2':self.reg()
                    elif INPUT=='0':self.quit()
                    else:print '未知命令!'
                except KeyboardInterrupt:
                    print '强制退出!'
                    sys.exit()

##登录用户
        elif result=='true':
            print LOGIN
            self.addrDB='/root/address/%s.db' %self.name
            if os.path.isfile(self.addrDB)==False: #判断用户数据库文件是否存在
                p.dump('',open(self.addrDB,'w'))
                self.addr_read_dict = {}
                self.addr_dict = {}
            try:
                self.addr_read_dict=p.load(open(self.addrDB))
                self.addr_dict=p.load(open(self.addrDB))
                if not self.addr_dict:
                    self.addr_dict={}
                    self.addr_read_dict = {}
            except EOFError: #文件存在,但内容为空的异常处理
                self.addr_read_dict = {}
                self.addr_dict = {}
            while True:
                try:
                    INPUT=raw_input('>')
                    if not INPUT:pass
                    elif INPUT=='f':self.search()
                    elif INPUT=='a':self.add()
                    elif INPUT=='d':self.delete()
                    elif INPUT=='l':self.listall()
                    elif INPUT=='m':self.modify()
                    elif INPUT=='e':self.empty()
                    elif INPUT=='o':self.logout()
                    elif INPUT=='s':self.save()
                    elif INPUT=='h':print LOGIN
                    elif INPUT=='q':self.quit()
                    else:print '未知命令!'
                except KeyboardInterrupt: #强退的异常处理
                    try:
                        INPUT = raw_input('强制退出,所有未保存信息均会丢失!\n继续ctrl+c强制退出,按任意键返回: ')
                    except:
                        print '强制退出!'
                        sys.exit()


INFO=address()
if __name__ == '__main__':
    INFO.menu()


相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载