DEV Community

Israel Manzo
Israel Manzo

Posted on

🐍 Python CRUD OOP todo list...

The most common and basic way to start learning a new programing language is creating a simple application, in this case Todo List
Any developer can build one, different ways-methods to create an efficient way to keep your task on the line.

Let's use Python to accomplish it.

Todo list es el mas comun y basico procedimiento para aprender a programar. Crearemos un todo list usando OOP.

  • Let's build a Todo class and the way, where to contain each todo.
class Todo:
  todos = []
Enter fullscreen mode Exit fullscreen mode
  • Let's build a method to add a todo into the list

add todo in the list

  def add_todo(self, todo):
Enter fullscreen mode Exit fullscreen mode
  • Let's delete when the user inputs the number of the list index

Ahora implementaremos como borrar y poner un check en cada tarea de la lista. Primero, como borrar un todo usando us index o el nombre.

def delete_todo_by_index(self, index):
   print('List is empty') if len(self.todos) == 0 else self.todos.remove(self.todos[index])
Enter fullscreen mode Exit fullscreen mode

What is the user inputs to delete by the todo name.

# list is empty else remove todo
  def delete_todo(self, todo):
     print('List is empty') if self.todos == [] else self.todos.remove(todo)
Enter fullscreen mode Exit fullscreen mode

Great! We know how to delete by name and index each todo, now let's not delete it but check it when is completed.

Ahora vamos a poner un check por cada todo. El boolean es la accion del usuario en chequear o no el todo.

# iterate in list and check for todo then check if todo is correct
  def checked_todo(self, isChecked, todoCheck):
    for todo in self.todos:
      if todoCheck == todo: isChecked = True
      print('(x)', todoCheck) if isChecked is True else print('( )', todoCheck) 
      break # this line will stop the iteration as soon the todo is checked, else will iterate the whole list even if the todo is at the beginning.. 
Enter fullscreen mode Exit fullscreen mode

The boolean YES || NOT toggles depending on the user input...

Let's display all todos...

# interate in todos list and return each todo
  def displayTodos(self):
     todo = [print(todo) for todo in self.todos ]
     return todo
Enter fullscreen mode Exit fullscreen mode

Let's display the todos in the console window, first & last todo.

# given position desired on list
  def get_first_or_last_todo(self, position):
     if self.todos == []: print('List is empty')
     if position == 'first':
       return self.todos[0]  
     elif position == 'last':
       return self.todos[-1]
       return self.todos
Enter fullscreen mode Exit fullscreen mode

Great! Now to complete the CRUD on this todo list, we are going to implement how to update a todo.

# iterate in list and check for todo then update
  def update_todo(self, old_todo, new_todo):
     for i in self.todos:
       if i == old_todo: old_todo = new_todo
       else: print('Todo is not in the list')
Enter fullscreen mode Exit fullscreen mode

Bien, ahora para completar esta lista tenemos que actualizar cada todo.

This is great..! We have a CRUD todo list. There is many ways to create todo list using any programming language.
Maybe this is not as good it should be but to give an idea on how to implement a basic todo list.

If you like or hate the post, I will appreciate any constructive feedback.
In the future I am going to implement a todo list in a real iOS application on programmatically, storyboard or SwiftUI method, using CRUD with fake, local or remote database.

Hay muchas maneras de escribir un todo list, quizas esta no es la mejor manera pero la idea es como empezar con algo muy basico para entender como implementar un todo list.

Cheers.. 🍺 && Happy Coding

Top comments (1)

qyuzet profile image

May I know the implementation code of it? I mean the script when u add todo in this program