Learn practical skills, build real-world projects, and advance your career

MINI PROJECT- TODO APP

TEAM 2

import json
import pprint


class Project:
    # initialization
    def __init__(self):
        
        self.toDoTasks = list()
        self.tags = dict()
        self.DATA_DICTIONARY = {"TodoList": {},
                                "TodoTasks":self.toDoTasks,
                                "Hashtags":self.tags
                                   
                                   }

    # Creating a New List
    def create_new_todo(self, list_items="", list_name=""):
        if not list_name:
            list_name = input("Enter the todo list name: ")
        if list_name not in self.DATA_DICTIONARY["TodoList"]:
            self.DATA_DICTIONARY["TodoList"][list_name] = {"items": {}}
        if not list_items:
            list_items = input("Enter items and quantity (ex: apples-4 banana-6 mango): ")  # apples-4 banana-6 mango cucumber-10
        for items in list_items.split(" "):
            if len(items.split("-")) > 1:
                self.DATA_DICTIONARY["TodoList"][list_name]["items"][items.split("-")[0]] = {
                                                                                            "quantity": items.split("-")[1],
                                                                                            "status": False}

            else:
                self.DATA_DICTIONARY["TodoList"][list_name]["items"][items.split("-")[0]] = {"quantity": "",
                                                                                             "status": False}

        print(f"After adding the list, todo list:\n {self.DATA_DICTIONARY['TodoList']}")
                
    def show_all_lists(self):
        result = list(self.DATA_DICTIONARY["TodoList"].keys())
        print("list names are: ", result)

    # {'item1': {"quantity": "int", "status": "True/False"}
    def display_list(self, list_name=""):
        result = []
        if not list_name:
            list_name = input("Enter the todo list name to display: ")
        if list_name in self.DATA_DICTIONARY["TodoList"]:
            items_dict =self.DATA_DICTIONARY["TodoList"][list_name]["items"]
            for item_name in items_dict.keys():
                print("Name :", item_name, "Quantity :", items_dict[item_name]["quantity"], "Done?:",
                  items_dict[item_name]["status"])
        else:
            print("Please enter the valid list name")

        
    def delete_list(self,list_name=""):
        if not list_name:
            list_name = input("Enter the todo list name to delete_list: ")
        if list_name in self.DATA_DICTIONARY["TodoList"]:
            del self.DATA_DICTIONARY["TodoList"][list_name]
            print("Deleted Successfully")
        else:
            print("List_name not found to be deleted")
    
    #########
    def hasTag(self,task):
        tags = []
        for word in task.split():
            if word[0]=="#":
                if word not in tags:
                    tags.append(word)
        if len(tags)!=0:
            return tags
        return None
    def addTask(self,content,isTagged=False,tags=[]):
        content+=" Pending"
        self.toDoTasks.append(content)
        if isTagged :
            for tag in tags:
                lis = self.tags.get(tag,[])
                lis.append(content)
                self.tags[tag] = lis
        print("Task added successfully..")
    
    def fetchAndForwardTasks(self,choice):
        tasks = self.toDoTasks
        actions = ["mark as complete","edit the content","delete the task"]
        n = len(tasks)
        if n!=0:
            for i in range(n):
                if choice==6:
                    if tasks[i].split()[-1]=="Pending":
                        print(i+1,tasks[i])
                    continue
                else:
                    print(i+1,tasks[i])
            print("pick the task no to",actions[choice-6])
            index = int(input())
            if index>=1 and index<=n:
                if choice==6:
                    self.markAsComplete(index-1)
                elif choice==7:
                    content = input("Enter new content :\n")
                    self.editTask(index-1,content)
                elif choice==8:
                    self.deleteTask(index-1)
            else:
                print("Invalid Selection..")
        else:
            print("tasks unavailable..")
    def markAsComplete(self,index):
        oldContent = self.toDoTasks[index]
        content = ' '.join(self.toDoTasks[index].split()[:-1])
        content+=" Completed"
        for tag in self.tags:
            if oldContent in self.tags[tag]:
                self.tags[tag].remove(oldContent)
                self.tags[tag].append(content)
        self.toDoTasks[index] = content
        print("Task marked as completed")
    def editTask(self,index,content):
        self.deleteTask(index)
        tagName = self.hasTag(content)
        if tagName:
            self.addTask(content,True,tagName)
        else:
            self.addTask(task)
        print("modified..")
    def deleteTask(self,index):
        content = self.toDoTasks[index]
        self.toDoTasks.pop(index)
        temp = dict(self.tags)
        for tag in temp:
            if content in temp[tag]:
                temp[tag].remove(content)
            if len(temp[tag])==0:
                del self.tags[tag]
            else:
                self.tags[tag] = temp[tag]
        
        print("Task deleted Successfully..")
    def fetchTaggedTasks(self):
        tags = self.tags
        for tag in tags:
            print(tag)
            for task in self.tags[tag]:
                print(task)
    def fetchTaskByTag(self,tag):
        lis = []
        tags = self.tags
        if tag in tags:
            print("Tasks pinned with tag {} are".format(tag))
            for task in self.tags[tag]:
                print(task)
        else:
            print("No results found for tag",tag)


def show_menu():
    print("\n***** Select Your Choice *****\n")
    print(f' 1.create new todo list\n 2.show_all_lists\n 3.display_list \n 4. delete_list ')
          
    print(f' 5.add Task\n 6.Mark as Complete\n 7.edit task \n 8.delete Task\n 9.to view tagged tasks\n 10.Search by tag')
if __name__ == '__main__':
    obj = Project()
       
    while True: 
        show_menu()
        ch = int(input("Enter Choice : "))
        if ch==1:
            obj.create_new_todo()
        elif ch==2:
            obj.show_all_lists()
        elif ch==3:
            obj.display_list()
        elif ch==4:
            obj.delete_list()
        elif ch==5:
            task = input("Enter the task :\n")
            tagName = obj.hasTag(task)
            if tagName:
                obj.addTask(task,True,tagName)
            else:
                obj.addTask(task)
        elif ch>=6 and ch<=8:
            obj.fetchAndForwardTasks(ch)
        elif ch==9:
            obj.fetchTaggedTasks()
        elif ch==10:
            tag = input("Enter tag name with #")
            obj.fetchTaskByTag("#"+tag)
        else:
            print("Exited!")
            with open('D:/Python CN/userdata.txt', 'w') as userfile:
                userfile.write(json.dumps(obj.DATA_DICTIONARY))
                #userfile.write(json.dumps(obj.toDoTasks))
                #userfile.write(json.dumps(obj.tags))
                break
            
    
***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 1 Enter the todo list name: Grocery Enter items and quantity (ex: apples-4 banana-6 mango): apples-4 banana-12 grapes-36 mango After adding the list, todo list: {'Grocery': {'items': {'apples': {'quantity': '4', 'status': False}, 'banana': {'quantity': '12', 'status': False}, 'grapes': {'quantity': '36', 'status': False}, 'mango': {'quantity': '', 'status': False}}}} ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 2 list names are: ['Grocery'] ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 3 Enter the todo list name to display: Grocery Name : apples Quantity : 4 Done?: False Name : banana Quantity : 12 Done?: False Name : grapes Quantity : 36 Done?: False Name : mango Quantity : Done?: False ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 5 Enter the task : #anniversary buy gift for mom Task added successfully.. ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 5 Enter the task : #anniversary call caterer Task added successfully.. ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 5 Enter the task : I have to get the tap repaired #repair Task added successfully.. ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 5 Enter the task : I have to get AC repaired #repair Task added successfully.. ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 9 #anniversary #anniversary buy gift for mom Pending #anniversary call caterer Pending #repair I have to get the tap repaired #repair Pending I have to get AC repaired #repair Pending ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 10 Enter tag name with #anniversary Tasks pinned with tag #anniversary are #anniversary buy gift for mom Pending #anniversary call caterer Pending ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 6 1 #anniversary buy gift for mom Pending 2 #anniversary call caterer Pending 3 I have to get the tap repaired #repair Pending 4 I have to get AC repaired #repair Pending pick the task no to mark as complete 2 Task marked as completed ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 6 1 #anniversary buy gift for mom Pending 3 I have to get the tap repaired #repair Pending 4 I have to get AC repaired #repair Pending pick the task no to mark as complete 4 Task marked as completed ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 8 1 #anniversary buy gift for mom Pending 2 #anniversary call caterer Completed 3 I have to get the tap repaired #repair Pending 4 I have to get AC repaired #repair Completed pick the task no to delete the task 2 Task deleted Successfully.. ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 8 1 #anniversary buy gift for mom Pending 2 I have to get the tap repaired #repair Pending 3 I have to get AC repaired #repair Completed pick the task no to delete the task 3 Task deleted Successfully.. ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 7 1 #anniversary buy gift for mom Pending 2 I have to get the tap repaired #repair Pending pick the task no to edit the content 2 Enter new content : I have to get the tap repaired ASAP #repair Task deleted Successfully.. Task added successfully.. modified.. ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 9 #anniversary #anniversary buy gift for mom Pending #repair I have to get the tap repaired ASAP #repair Pending ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 4 Enter the todo list name to delete_list: Grocery Deleted Successfully ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 3 Enter the todo list name to display: Grocery Please enter the valid list name ***** Select Your Choice ***** 1.create new todo list 2.show_all_lists 3.display_list 4. delete_list 5.add Task 6.Mark as Complete 7.edit task 8.delete Task 9.to view tagged tasks 10.Search by tag Enter Choice : 11 Exited!