我在我的wxpython应用程序中做了一个弹出密码框的函数。密码,存在于对话框.py,如下所示:
def password_dialog(self, password):
# Only ask for password if it actually exist
if password == 'False':
return True
question = 'Put in password:'
dialog = wx.PasswordEntryDialog(self, question, 'Password...')
if dialog.ShowModal() == wx.ID_OK:
if dialog.GetValue() == password:
dialog.Destroy()
return True
else:
dialog.Destroy()
__wrong_pass()
raise WrongPassword
else:
dialog.Destroy()
raise CancelDialog
例外情况在同一个文件中:
class WrongPassword(Exception):
pass
class CancelDialog(Exception):
pass
在我的主程序中,我有一些类似这样的方法:
def on_sort_songs(self, event):
"""Renumbering the database and sort in artist and title order"""
# Check for password first
try:
dialogs.password_dialog(self, opts.generic['password'])
except dialogs.CancelDialog:
return
except dialogs.WrongPassword:
return
# Sort database and repopulate GUI
self.jbox.sort_songs()
self.populate_songlist()
它工作正常。但这似乎不是一个很好的和python的方式来处理密码对话框。还是真的?你知道吗
我不认为你的对话框函数应该在这种情况下引发异常。根据验证是否通过,让它返回True或False。那么你需要做的就是:
例外情况只有在您想要区分的其他随机故障情况下才有必要,例如“AuthenticationServer is Down”
我认为在这种情况下返回True或False是好的另一个原因是,这样您就可以使用可以交换的模块化身份验证方法。例如,django如何使用一个返回布尔值的is\u authenticated()方法。最终用途只需担心其是否经过身份验证。不是它具体引发的各种异常,比如对话框被关闭。有些情况下甚至可能不使用对话框..可能是命令行或web界面等
相关问题 更多 >
编程相关推荐