用于简单 GTD 应用程序的 Python 类 [英] Python classes for simple GTD app

查看:80
本文介绍了用于简单 GTD 应用程序的 Python 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为自己编写一个非常基本的 GTD 应用程序,不仅是为了组织起来,而且是为了更好地编码和 Python.但是,我在上课时遇到了一些麻烦.

I'm trying to code a very rudimentary GTD app for myself, not only to get organized, but to get better at coding and get better at Python. I'm having a bit of trouble with the classes however.

以下是我目前的课程:

class Project:
    def __init__(self, name, actions=[]):
        self.name = name
        self.actions = actions
    def add(self, action):
        self.actions.append(action)

class Action:
    def __init__(self, do='', context=''):
        self.do = do
        self.context = context

每个项目都有它的动作,但是我想这样做,以便项目也可以包含其他项目.每天说我想打印出所有内容的列表.我在想出如何构建一个看起来像这样的列表时遇到了麻烦

Each project has actions to it, however I want to make it so that projects can also consist of other projects. Say daily I wanted to print out a list of everything. I'm having trouble coming up with how I would construct a list that looked like this

> Project A
>        Actions for Project A
>     Project B
>         Sub project A
>             Actions for Sub project A
>         Sub project B
>             Actions for Sub project B
>         Sub project C
>             Sub sub project A
>                 Actions for sub sub project A
>             Sub sub project B
>                 Actions for sub sub project B
>             Actions for Sub project C
>         Actions for Project B

我很清楚将使用递归.我正在纠结是否要创建另一个名为 SubProject 的类并将 Project 子类化到它.那里的东西只是让我的大脑引发了一个例外.

It's quite clear to me that recursion is going to be used. I'm struggling with whether to create another class called SubProject and subclass Project to it. Something there just makes my brain raise an exception.

我已经能够接受项目并将它们添加到 Project 类中的 actions 属性中,但是随后我遇到了 MegaProject.actions.action.actions.action 情况开始出现的地方.

I have been able to take projects and add them to the actions attribute in the Project class, however then I run into where MegaProject.actions.action.actions.action situations start popping up.

如果有人可以帮助解决类结构,将不胜感激!

If anyone could help out with the class structures, it would be greatly appreciated!

推荐答案

您可以创建一个子项目成员,类似于您的操作列表,并以类似的方式为其分配项目.不需要对 Project 进行子类化.

You could create a subprojects member, similar to your actions list, and assign projects to it in a similar way. No subclassing of Project is necessary.

class Project:
    def __init__(self, name, actions=[], subprojects=[]):
        self.name = name
        self.actions = actions
        self.subprojects = subprojects

    def add(self, action):
        self.actions.append(action)

    def add_project(self, project)
        self.subprojects.append(project)

更好的是,您可能想要实现一个组合模式,其中项目是组合,操作是叶子.

Even better, you may want to implement a composite pattern, where Projects are composites and Actions are leaves.

class Project:
    def __init__(self, name, children=[]):
        self.name = name
        self.children = children

    def add(self, object):
        self.children.append(object)

    def mark_done(self):
        for c in self.children:
            c.mark_done()

class Action:
    def __init__(self, do):
        self.do = do
        self.done = False

    def mark_done(self):
        self.done = True

这里的关键是项目具有与操作相同的界面(添加/删除方法除外).这允许递归调用整个树上的方法.如果你有一个复杂的嵌套结构,你可以在顶层调用一个方法,然后过滤到底层.

They key here is that the projects have the same interface as the actions (with the exception of the add/delete methods). This allows to to call methods on entire tree recursively. If you had a complex nested structure, you can call a method on the top level, and have it filter down to the bottom.

如果您想要一种方法来获取树中所有叶节点的平面列表(操作),您可以在 Project 类中实现这样的方法.

If you'd like a method to get a flat list of all leaf nodes in the tree (Actions) you can implement a method like this in the Project class.

def get_action_list(self):
    actions = []
    for c in self.children:
        if c.__class__ == self.__class__:
            actions += c.get_action_list()
        else:
            actions.append(c)
    return actions

这篇关于用于简单 GTD 应用程序的 Python 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆