在类之间发送失败消息
好吧,这实际上只是一个概念问题,我遇到了一些麻烦,而不是编码问题。我正在构建一个简单的每周时间跟踪器来自动化游戏中的一些管理。所以,我有一个 TimeSheet 类和一个 TimeBlock 类。这是带有注释的时间表类,我不知道该怎么做。
class Timesheet():
def __init__(self,person,week):
self.person = person
self.week = week
self.hours = 168
self.time_blocks = []
def add_time_block(self,time_block):
##Test that the time_block actually fits in this week
if sum([x.hours for x in self.time_blocks]) >= self.hours-time_block.hours:
self.time_blocks.append(time_block)
else: ##This is where I'm not sure what I should do.
这不是一个停止显示的错误,它可能是一个 PEBKAC 错误,所以我认为 Exception 类已经消失了。我想要一种让 TimeSheet 说“不”的方法,而不需要将其耦合到特定的界面(这样我现在可以仅使用 CLI 来使用它,但一旦我学会编写 GUI 或 Web 界面,我最终会添加一个 GUI)就像移植到 3.0 一样)。这不应该破坏程序,但它应该提醒用户他们已经尝试了一些无法完成的事情,并且他们需要在尝试插入它之前缩短该时间块,修改一些其他时间块以使这个时间块适合,或者只是提交时间表,因为时间表已满。有哪些干净、优雅的方法可以做到这一点?
Ok, so this is really just a conceptual question I'm having a bit of trouble with rather than a coding problem. I'm building a simple, weekly time tracker to automate some admin in a game. So, I have a TimeSheet class and a TimeBlock class. Here is the Timesheet class with a comment where I'm not sure what to do.
class Timesheet():
def __init__(self,person,week):
self.person = person
self.week = week
self.hours = 168
self.time_blocks = []
def add_time_block(self,time_block):
##Test that the time_block actually fits in this week
if sum([x.hours for x in self.time_blocks]) >= self.hours-time_block.hours:
self.time_blocks.append(time_block)
else: ##This is where I'm not sure what I should do.
This isn't a show-stopping error, it's probably a PEBKAC error, so I think the Exception class is out. What I want a way for the TimeSheet to say 'no' without coupling it to a specific interface (so I could use this with just CLI for now but eventually add a GUI once I learn to code GUIs or a web interface once one that I like ports to 3.0). This shouldn't break the program, but it should alert the user that they've attempted something that cannot be done and they need to shorten that timeblock before they try to insert it, modify some other timeblocks to make this one fit, or just submit their timesheet because the timesheet is full. What are some clean, elegant ways to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用异常。您始终可以将 CLI 支持代码中的函数调用包装在 try... except 块中,并在那里做出适当的反应;我认为使用这种方法没有任何缺点。
I would use an Exception. You can always wrap the function call from within the CLI supporting code in a try...except block and react there appropriately; I see no drawbacks using this methodology.
您可以提供一个事件侦听器接口,“感兴趣的各方”可以在其中注册以获取您生成的事件,例如在上面的
else
中。拥有一个商定的Event
接口可以让您对这些事件做出明智的反应,例如拥有一个事件类型(枚举)或一个UserAlertEvent
子类。You could provide an event listener interface, where "interested parties" can register to get events that you generate for example in your
else
above. Having an agreed-uponEvent
interface allows you to then react on these intelligently, e.g. have an event type (enumeration) or aUserAlertEvent
subclass.