Complete Implementation
Python
import json
from pathlib import Path
from datetime import datetime
from dataclasses import dataclass, asdict
TODO_FILE = Path("todos.json")
@dataclass
class Task:
id: int
title: str
done: bool = False
priority: str = "medium"
created: str = ""
def __post_init__(self):
if not self.created:
self.created = datetime.now().strftime("%Y-%m-%d %H:%M")
class TodoApp:
def __init__(self):
self.tasks: list[Task] = []
self._next_id = 1
self.load()
def load(self):
if TODO_FILE.exists():
data = json.loads(TODO_FILE.read_text())
self.tasks = [Task(**t) for t in data["tasks"]]
self._next_id = data.get("next_id", len(self.tasks) + 1)
def save(self):
TODO_FILE.write_text(json.dumps(
{"tasks": [asdict(t) for t in self.tasks], "next_id": self._next_id}, indent=2
))
def add(self, title, priority="medium"):
task = Task(id=self._next_id, title=title, priority=priority)
self.tasks.append(task); self._next_id += 1; self.save(); return task
def complete(self, task_id):
task = next((t for t in self.tasks if t.id == task_id), None)
if task: task.done = True; self.save()
return task
def delete(self, task_id):
task = next((t for t in self.tasks if t.id == task_id), None)
if task: self.tasks.remove(task); self.save(); return True
return False
app = TodoApp()
ICONS = {"high": "π΄", "medium": "π‘", "low": "π’"}
while True:
print("\n[1]List [2]Add [3]Complete [4]Delete [q]Quit")
c = input("> ").strip().lower()
if c == "q": break
elif c == "1":
for t in sorted(app.tasks, key=lambda x: {"high":0,"medium":1,"low":2}[x.priority]):
print(f" [{t.id}] {'β' if t.done else 'β'} {ICONS[t.priority]} {t.title}")
elif c == "2":
title = input("Title: "); p = input("Priority [medium]: ") or "medium"
task = app.add(title, p); print(f"Added #{task.id}")
elif c == "3":
tid = int(input("Task ID: "))
print("Done!" if app.complete(tid) else "Not found.")
elif c == "4":
tid = int(input("Task ID: "))
print("Deleted." if app.delete(tid) else "Not found.")Tip: Extend this project with due dates and tags, then wrap it in a Flask web app for a full-stack portfolio piece.