在类之间发送失败消息

发布于 2024-11-28 03:48:18 字数 894 浏览 1 评论 0原文

好吧,这实际上只是一个概念问题,我遇到了一些麻烦,而不是编码问题。我正在构建一个简单的每周时间跟踪器来自动化游戏中的一些管理。所以,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-12-05 03:48:19

我会使用异常。您始终可以将 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.

秉烛思 2024-12-05 03:48:19

您可以提供一个事件侦听器接口,“感兴趣的各方”可以在其中注册以获取您生成的事件,例如在上面的 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-upon Event interface allows you to then react on these intelligently, e.g. have an event type (enumeration) or a UserAlertEvent subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文