I tend to spend my energy and time on this instead of wasting on nonsense! Now you may think what it is, so think more.
import json
import os
from datetime import datetime
class Task:
def __init__(self, title, completed=False, created_at=None):
self.title = title
self.completed = completed
self.created_at = created_at or
datetime.now().isoformat()
def complete(self):
self.completed = True
def to_dict(self):
return {
"title": self.title,
"completed": self.completed,
"created_at": self.created_at,
}
@classmethod
def from_dict(cls, data):
return cls(
title=data["title"],
completed=data["completed"],
created_at=data["created_at"],
)
class TaskManager:
def __init__(self, filename="tasks.json"):
self.filename = filename
self.tasks = []
self.load_tasks()
def add_task(self, title):
task = Task(title)
self.tasks.append(task)
self.save_tasks()
def list_tasks(self):
if not self.tasks:
print("No tasks available.")
return
for index, task in enumerate(self.tasks, start=1):
status = "✓" if task.completed else "✗"
print(f"{index}. [{status}] {task.title}")
def complete_task(self, index):
if 0 <= index < len(self.tasks):
self.tasks[index].complete()
self.save_tasks()
print("Task completed.")
else:
print("Invalid task number.")
def delete_task(self, index):
if 0 <= index < len(self.tasks):
removed = self.tasks.pop(index)
self.save_tasks()
print(f"Deleted: {removed.title}")
else:
print("Invalid task number.")
def search_tasks(self, keyword):
results = [
task for task in self.tasks
if keyword.lower() in task.title.lower()
]
if not results:
print("No matching tasks found.")
return
print("\nSearch Results:")
for task in results:
status = "Completed" if task.completed else "Pending"
print(f"- {task.title} ({status})")
def show_statistics(self):
total = len(self.tasks)
completed = sum(task.completed for task in self.tasks)
pending = total - completed
print("\nStatistics")
print("-" * 30)
print(f"Total Tasks : {total}")
print(f"Completed Tasks : {completed}")
print(f"Pending Tasks : {pending}")
if total > 0:
percentage = (completed / total) * 100
print(f"Completion Rate : {percentage:.2f}%")
def save_tasks(self):
with open(self.filename, "w", encoding="utf-8") as file:
json.dump(
[
task.to_dict() for task in self.tasks],
file,
indent=4
)
def load_tasks(self):
if not os.path.exists(self.filename):
return
try:
with open(self.filename, "r", encoding="utf-8") as file:
data = json.load(file)
self.tasks = [
Task.from_dict(item)
for item in data
]
except Exception as error:
print(f"Failed to load tasks: {error}")
def print_menu():
print("\nTask Manager")
print("=" * 40)
print("1. Add Task")
print("2. List Tasks")
print("3. Complete Task")
print("4. Delete Task")
print("5. Search Tasks")
print("6. Show Statistics")
print("7. Exit")
def main():
manager = TaskManager()
while True:
print_menu()
choice = input("Select an option: ").strip()
if choice == "1":
title = input("Task title: ").strip()
oice == "