Composite template for GTD application

This is a continuation of one of my previous questions.

Here are my classes.

#Project class     
class Project:
    def __init__(self, name, children=[]):
        self.name = name
        self.children = children
    #add object
    def add(self, object):
        self.children.append(object)
    #get list of all actions
    def actions(self):
        a = []
        for c in self.children:
            if isinstance(c, Action):
                a.append(c.name)
        return a
    #get specific action
    def action(self, name):
        for c in self.children:
            if isinstance(c, Action):
                if name == c.name:
                    return c
    #get list of all projects
    def projects(self):
        p = []
        for c in self.children:
            if isinstance(c, Project):
                p.append(c.name)
        return p
    #get specific project
    def project(self, name):
        for c in self.children:
            if isinstance(c, Project):
                if name == c.name:
                    return c

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

    def mark_done(self):
        self.done = True

This is the trouble that I am experiencing. If I create a large project with several small projects, I want to see what projects or actions are for the current project, however I get all of them in the tree. Test code is used here (note that I deliberately chose several different ways to add projects and actions for verification, to make sure that the different methods work).

life = Project("life")

playguitar = Action("Play guitar")

life.add(Project("Get Married"))

wife = Project("Find wife")
wife.add(Action("Date"))
wife.add(Action("Propose"))
wife.add(Action("Plan wedding"))
life.project("Get Married").add(wife)

life.add(Project("Have kids"))
life.project("Have kids").add(Action("Bang wife"))
life.project("Have kids").add(Action("Get wife pregnant"))
life.project("Have kids").add(Project("Suffer through pregnancy"))
life.project("Have kids").project("Suffer through pregnancy").add(Action("Drink"))
life.project("Have kids").project("Suffer through pregnancy").add(playguitar)

life.add(Project("Retire"))
life.project("Retire").add(playguitar)

There should be several projects in life, within which there are several projects. The structure is roughly something like this (where the indentation is - projects and - actions)

Life
    Get Married
        Find wife
            - Date
            - Propose
            - Plan wedding
    Have kids
        - Bang wife
        - Get wife pregnant
        Suffer through pregnancy
            - Drink
            - Play guitar
    Retire
        - Play guitar

, , life.actions() , . life.projects() , , "Get Married", "Have kids" "Retire". ?

+3
1

:

 __init__(self, name, children=[]):

, , . . . None , None.

 __init__(self, name, children=None):
    if children is None:
       children = []
+5

Source: https://habr.com/ru/post/1713512/


All Articles