The TODO app revisited
For starters, let’s revisit the TODO app we’ve built in Python Fundamentals I. Here it is, in its full glory:
exit = False
todo_items = []
def get_argument(command):
    command_name, argument = command.split(" ", maxsplit=1)
    return argument
def handle_add_command(command):
    task = get_argument(command)
    print(f"Adding task: {task}")
    todo_items.append(task)
def handle_delete_command(command):
    item_number = get_argument(command)
    print(f"Deleting task: {item_number}")
    todo_items.pop(int(item_number) - 1)
def handle_list_command():
    index = 0
    for todo_item in todo_items:
        index += 1
        print(f"{index}. {todo_item}")
def handle_open_command(command):
    # Read a list of todo items from a file, one per line
    filename = get_argument(command)
    filename += ".todo.txt"
    with open(filename, "r") as file:
        for line in file:
            todo_items.append(line.strip())
def handle_save_command(command):
    # Save the list to a file, one item per line
    filename = get_argument(command)
    filename += ".todo.txt"
    with open(filename, "w") as file:
        for todo_item in todo_items:
            file.write(f"{todo_item}\n")
def process_command(command):
    exit = False
    if command.startswith("exit"):
        exit = True
    elif command.startswith("add"):
        handle_add_command(command)
    elif command.startswith("delete"):
        handle_delete_command(command)
    elif command.startswith("list"):
        handle_list_command()
    elif command.startswith("open"):
        handle_open_command(command)
    elif command.startswith("save"):
        handle_save_command(command)
    else:
        print("Command not recognized")
    return exit
while not exit:
    command = input("Enter command: ")
    exit = process_command(command)
Code language: Python (python)
Some notes on the app
When looking back at it, we could group most of the lines into three categories:
- Some administrative variables (exit, todo_items)
 - A list of function definitions
 - The main loop
 
We’ll convert this little application into a proper Python package in the following lessons. As a bonus, we’ll also convert the project into a Poetry project once it’s done. Perhaps we’ll even throw in some colors while we’re at it!
