最Pythonic的方式来处理对话框?
我在 wxpython 应用程序中创建了一个弹出密码框的函数。 dialogs.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式方法。或者是吗?
I made a function in my wxpython app that pop up a passwordbox. The code, that's present in dialogs.py, looks like this:
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
The exceptions is in the same file:
class WrongPassword(Exception):
pass
class CancelDialog(Exception):
pass
In my main program I then have some methods that look something like this:
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()
It work ok. But it don't seems like a very good and pythonic way to handle password dialogs. Or is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在这种情况下你的对话框函数不应该引发异常。只需让它根据验证是否通过返回 True 或 False 即可。那么您需要做的就是:
仅对于您想要区分的其他随机故障情况才需要异常,例如“身份验证服务器已关闭”
我认为在这种情况下返回 True 或 False 的另一个原因是因为您可以使用可更换的模块化身份验证方法。例如 django 如何使用返回布尔值的单个 is_authenticated() 方法。最终用户只需要关心它是否经过认证。不是它具体引发的各种异常,例如对话框被关闭。有些情况甚至可能不使用对话框..可能是命令行,或网络界面等。
I dont think your dialog function should be raising exceptions in this case. Just have it return True or False depending on whether the validation passes or not. Then all you need to do is:
Exceptions would only be necessary for other random failure cases that you want to distinguish, such as "Authentication Server is Down"
Another reason I think its good to return True or False in this case is because then you can use modular authentication methods that can be swapped out. Such as how django uses a single is_authenticated() method that returns a boolean. The end use only needs to worry about whether its authenticated or not. Not what various exceptions it raises specifically, like a dialog being closed. Some cases might not even use a dialog..maybe a command line, or a web interface ,etc.