Python classes for simple GTD app

此生再无相见时 提交于 2019-12-06 12:13:29

问题


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.

Here are the classes I have so far:

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

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.

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!


回答1:


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.

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



回答2:


I suggest you look at the composite pattern which can be applied to the "Project" class. If you make your structure correctly, you should be able to make action be a leaf of that tree, pretty much like you described in your example.

You could, for instance, do a Project class (abstract), a ProjectComposite class (concrete) and your action class as a leaf.




回答3:


Have you evaluated existing GTD tools? I'd look at file formats used by existing GTD tools, esp. those that save to XML. That would give you an idea about which ways to organize this kind of data tend to work.

Structure ranges from rather simplistic (TaskPaper) to baroque (OmniFocus). Look around and learn what others did before you.



来源:https://stackoverflow.com/questions/1175110/python-classes-for-simple-gtd-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!