Cleanhun 发表于 2020-7-3 21:07:32

求助数据储存优化及代码优化(用户登录模块)

PS:该程序有一默认锁定的用户(即所读取的文件均不为空)
源代码如下:
import easygui as g

def count_users():
    count = len(dict_members)
    return count

def secure_question(read_write):
    if read_write == 'read':
      with open('securequestions.pkl','rb') as read_file:
            (questions,answer) = pickle.load(read_file)
            return (questions,answer)
      
    if read_write == 'write':
      (list_question,list_answer) = secure_question('read')
      msg = '请设置账号密保问题'
      title = '账号安全设置'
      choices = ('密保问题','答案')
      while True:
            (str_question,str_answer) = g.multenterbox(msg,title,choices)
            if len(str_question) and len(str_answer):
                break
            else:
                msg = '输入不能为空,请重新输入'
      list_question.append(str_question)
      list_answer.append(str_answer)
      question_data =
      with open('securequestions.pkl','wb') as write_file:
            pickle.dump(question_data,write_file)
      g.msgbox('密保问题设置成功','安全通知')
      
      
   
def read_user_ID(file_name):
    pickle_file = open(file_name,'rb')
    dict_user = pickle.load(pickle_file)
    pickle_file.close()
    return dict_user

def read_password():
    with open('password.pkl','rb') as passwd_file:
      passwd = pickle.load(passwd_file)
      return passwd

def write_password(password):
    passwd = read_password()
    passwd.append(password)
      
    with open('password.pkl','wb') as passwd_file:
      pickle.dump(passwd,passwd_file)
      
      

def write_file(name,user_ID):
    dict_members = user_ID
    pickle_file = open('data.pkl','wb')
    pickle.dump(dict_members,pickle_file)
    pickle_file.close()


def changepasswd(name):
    (question,answer) = secure_question('read')
    flag = dict_members
    msg = question
    correct_answer = answer
    title = '修改密码窗口'
    while True:
      enter_answer = g.enterbox(msg,title)
      if enter_answer == correct_answer:
            msg = '请输入新密码'
            new_password = g.passwordbox(msg,title)
            password_list = read_password()
            password_list = new_password
            with open('password.pkl','wb') as passwd_file:
                pickle.dump(password_list,passwd_file)
            break
      else:
            msg = question
            msg = '输入错误,请重新输入'+'\n'+msg

def find_password():
    msg = '请输入您的用户名'
    title = '找回密码'
    name = g.enterbox(msg,title)
    if name in dict_members:
      changepasswd(name)
    else: g.msgbox('您输入的用户名不存在','提示')
      
   
   
def main_menu():
    title = '主界面'
    choices = ('修改密码','注销','退出')
    while True:
      option = g.buttonbox('',title,choices)
      if option == '修改密码':
            changepasswd(current_user)
      if option == '注销':
            return False
      if option == '退出':
            return True

def frozen_users(read_write,name):
    if read_write == 'read':
      with open('frozen.pkl','rb') as frozend_file:
            frozen_user = pickle.load(frozend_file)
            return frozen_user

    ifread_write == 'write':
      Name = name
      frozen_list = frozen_users('read',Name)
      frozen_list.append(name)
      with open('frozen.pkl','wb') as frozen_file:
            pickle.dump(frozen_list,frozen_file)


def frozen_off(name):
    user_ID = dict_members
    (questions,answer) = secure_question('read')
    msg = questions
    title = '账号安全窗口'
    n = 3
    while n:
      answer_enter = g.enterbox(msg,title)
      if answer_enter == answer:
            frozen_list = frozen_users('read',name)
            frozen_list.remove(name)
            with open('frozen.pkl','wb') as frozen_file:
                pickle.dump(frozen_list,frozen_file)
            break
      else:
            n -= 1
            g.msgbox('输入错误,你还有%d次机会'%n,title)
            

def SignIn():
    msg = '请输入用户名和密码'
    title = '用户注册窗口'
    user_info = []
    user_info = g.multpasswordbox(msg,title,('用户名','密码'))
    (name,password) = user_info
    while name in dict_members:
      g.msgbox('用户名已存在请重新输入!')
      SignIn()
    else:
      user_ID = count_users()
      dict_members = user_ID
      
      write_password(password)
      write_file(name,user_ID)
      secure_question('write')
      return { name:user_ID }

def login(msg,title):
    while 1:
      user_info = []
      user_info = g.multpasswordbox(msg,title,('用户名','密码'))
      (name,password) = user_info

      if name not in dict_members:
            g.msgbox('用户名不存在请重新输入!')
            login(msg,title)
      else:
            break
    return user_info


def LogIn():
   
    msg = '请输入用户名和密码'
    title = '用户登录窗口'
    (Name,Password) = login(msg,title)
    if Name in frozen_users('read',Name):
      g.msgbox('该用户已冻结,请前往解冻')
      frozen_off(Name)
    else:
         global current_user
         current_user = Name
         user_ID = dict_members
         passwd = read_password()
         n = 3
         while (n-1):
            if Password == passwd:
               
                # (暂时不用) g.msgbox('欢迎进入XXOO系统,请点击右上角的X结束程序',ok_button = '退出')
                return True
                break
            else:
                n -= 1
                msg = '密码输入错误,你还有%d次机会' % n
                (Name,Password) = login(msg,title)
         else:
            g.msgbox('已输错三次密码,账号已锁定')
            frozen_users('write',Name)
            frozen_off(Name)
      
   

import pickle

dict_members = read_user_ID('data.pkl')
cmd = True
while cmd:
    title = '请选择操作对象'
    choices = ('注册','登录','忘记密码','退出')
    temp = g.buttonbox('',title,choices)

    if temp == '注册':
      newmember = SignIn()
      dict_members.update(newmember)
      g.msgbox('注册成功,赶紧登陆试试吧~')
      
    elif temp == '登录' :
      while LogIn():
            cmd = main_menu()
            if cmd:
                break

    elif temp == '忘记密码':
      find_password()

    elif temp == '退出' :
      break
      
g.msgbox('欢迎使用本程序',ok_button = '再见')
            
      


      
      

      
   








Twilight6 发表于 2020-7-3 21:16:28



先提个最基本的优化,你运行程序前先检测有无data.pkl文件,若无就新建,否则读取

Cleanhun 发表于 2020-7-4 18:28:54

Twilight6 发表于 2020-7-3 21:16
先提个最基本的优化,你运行程序前先检测有无data.pkl文件,若无就新建,否则读取

谢谢,会加进去的
页: [1]
查看完整版本: 求助数据储存优化及代码优化(用户登录模块)