Table of contents
No headings in the article.
Introduction
Recently, I embarked on a journey to build a simple ToDo List application using Python. What started as a fun project quickly turned into an invaluable learning experience. I deepened my understanding of Python’s core concepts and discovered striking similarities with other programming languages. Here are some key lessons I learned along the way.[
](github.com/your-username/todo-list-python)Working with Databases
I began by exploring Python’s built-in sqlite3
module. Connecting to and managing a database became a straightforward task once I got hands-on practice. This not only boosted my confidence in handling data persistence but also broadened my perspective on database integration in applications.
Here’s an example
import sqlite3
class DatabaseConnection:
def __init__(self):
self.db_connection = sqlite3.connect("db.py", check_same_thread=False)
self.db_cursor = self.db_connection.cursor()
pass
def db_init(self):
self.db_cursor.execute(
"""
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT,
completed BOOLEAN NOT NULL DEFAULT 0
)
"""
)
self.db_connection.commit()
db = DatabaseConnection()
# Export only the connection and cursor if needed externally
__all__ = ["db"]
I imported the sq3lite
module, created a class called DatabaseConnection
. Inside the class, we initialized an instance of the class, add database connection, and database cursor as properties. We also defined a method to handle initializing the database connection, and create tasks table, specifying columns and data type.
Mastering Error Handling
Delving further, I was able to have a hands-on practice using try-except
for robust error handling. Coming from the Javascript background, I discovered some similarities, in this case try-catch
in Javascript. It helped me understand that there are many similarities between programming languages yet to be explored. Here’s how I used it in this project.
try:
self.db.db_cursor.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
self.db.db_connection.commit()
return jsonify({"status": 200, "message": "Task deleted successfully!"})
except Exception as e:
return jsonify(
{"status": 500, "message": "An error occured", "error": str(e)}
)
Exploring Object-Oriented Programming
Creating classes in Python was another major milestone. I learned how to define a class and properly initialize it using the __init__()
method, with the self
parameter representing the instance. This exercise solidified my understanding of object-oriented principles and how they compare to similar concepts in other languages.
Understanding Python Modules
One fascinating insight was learning that every Python file is treated as a module. I discovered how to import these modules using the import
keyword and even how to import submodules using dot notation (e.g., from src.database import db
). This method is not only limited to custom modules but is also used when importing installed libraries, such as from flask import jsonify
.
Enhancing Conditional Logic
I also practiced using the if/elif
syntax in Python to manage multiple conditions—a syntax that mirrors JavaScript’s if/elseif
structure. This reinforced my understanding of control flow across different programming languages.
Building Web Routes with Flask
To bring the project to life, I experimented with Flask. I defined routes, managed HTTP methods, and learned how to handle parameters within my endpoints. This practical application of Flask not only improved my web development skills but also highlighted the versatility of Python in building scalable applications.
Diving into CI/CD
Finally, I took a deep dive into continuous integration and delivery (CI/CD) by implementing GitHub Actions. This step automated the assignment of PRs to authors, labeled PRs, and deleted branches, streamlining my development workflow and ensuring that each update was robust and reliable. As part of this process, I also learned how to use Docker to create containers and images, further enhancing the portability and scalability of my application. My Dockerfile looks like this:
# syntax=docker/dockerfile:1
FROM python:3.10-slim-buster
WORKDIR /todo
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"]
Conclusion
Building the ToDo List project was a multifaceted learning experience that enriched my Python knowledge, bridged concepts across languages, and introduced me to modern development practices. Whether you’re a beginner or a seasoned developer, I hope these insights inspire you to experiment and grow in your programming journey.
👉 Check out the project repository on GitHub: https://github.com/St80ene/To-Do-List-API
Let's Connect!
I'm always sharing updates, coding tips, and tech insights across my social channels. Follow me to stay in the loop and join the conversation:
GitHub: https://github.com/St80ene
Twitter: https://x.com/etienejames5
Follow me for more updates, tutorials, and behind-the-scenes peeks into my coding journey!