remove explicit code markers for reqs
This commit is contained in:
59
app.py
59
app.py
@@ -5,7 +5,6 @@ Simple Todo Application
|
||||
This application implements a command-line todo list manager that persists
|
||||
data to JSON and provides full CRUD operations with validation.
|
||||
|
||||
Implements requirements: REQ-001 through REQ-010
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -17,21 +16,6 @@ from datetime import datetime
|
||||
|
||||
|
||||
class TodoApp:
|
||||
"""
|
||||
Simple Todo Application with persistent storage.
|
||||
|
||||
Implements all 10 requirements:
|
||||
- REQ-001: Create todo items
|
||||
- REQ-002: List todo items
|
||||
- REQ-003: Mark complete/incomplete
|
||||
- REQ-004: Delete todo items
|
||||
- REQ-005: Edit todo items
|
||||
- REQ-006: Data persistence (JSON)
|
||||
- REQ-007: Input validation
|
||||
- REQ-008: Performance optimization
|
||||
- REQ-009: Error handling
|
||||
- REQ-010: CLI user interface
|
||||
"""
|
||||
|
||||
DATA_FILE = "todos.json"
|
||||
MAX_TITLE_LENGTH = 200
|
||||
@@ -42,7 +26,7 @@ class TodoApp:
|
||||
self._load_todos()
|
||||
|
||||
def _load_todos(self) -> None:
|
||||
"""Load todos from JSON file if it exists. REQ-006: Data Persistence."""
|
||||
|
||||
try:
|
||||
if Path(self.DATA_FILE).exists():
|
||||
with open(self.DATA_FILE, 'r') as f:
|
||||
@@ -52,16 +36,16 @@ class TodoApp:
|
||||
self.todos = []
|
||||
|
||||
def _save_todos(self) -> None:
|
||||
"""Save todos to JSON file. REQ-006: Data Persistence."""
|
||||
|
||||
try:
|
||||
with open(self.DATA_FILE, 'w') as f:
|
||||
json.dump(self.todos, f, indent=2)
|
||||
except IOError as e:
|
||||
raise RuntimeError(f"Failed to save todos: {e}") # REQ-009: Error handling
|
||||
raise RuntimeError(f"Failed to save todos: {e}")
|
||||
|
||||
def _validate_title(self, title: str) -> bool:
|
||||
"""
|
||||
Validate todo title. REQ-007: Input Validation.
|
||||
Validate todo title.
|
||||
|
||||
Args:
|
||||
title: The title to validate
|
||||
@@ -81,9 +65,8 @@ class TodoApp:
|
||||
|
||||
def create_todo(self, title: str, description: str = "") -> bool:
|
||||
"""
|
||||
Create a new todo item. REQ-001: Create Todo Items.
|
||||
REQ-007: Input Validation. REQ-009: Error handling.
|
||||
|
||||
Create a new todo item.
|
||||
|
||||
Args:
|
||||
title: The todo title
|
||||
description: Optional todo description
|
||||
@@ -108,13 +91,12 @@ class TodoApp:
|
||||
print(f"✅ Todo created: '{title}'")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating todo: {e}") # REQ-009: Error handling
|
||||
print(f"❌ Error creating todo: {e}")
|
||||
return False
|
||||
|
||||
def list_todos(self) -> None:
|
||||
"""
|
||||
Display all todos in a formatted list. REQ-002: List Todo Items.
|
||||
REQ-010: CLI user interface.
|
||||
Display all todos in a formatted list.
|
||||
"""
|
||||
if not self.todos:
|
||||
print("\n📋 No todos yet. Create one to get started!")
|
||||
@@ -139,7 +121,6 @@ class TodoApp:
|
||||
def mark_complete(self, todo_id: int) -> bool:
|
||||
"""
|
||||
Mark a todo as complete or toggle completion status.
|
||||
REQ-003: Mark Todo as Complete. REQ-009: Error handling.
|
||||
|
||||
Args:
|
||||
todo_id: The ID of the todo to mark complete
|
||||
@@ -161,14 +142,13 @@ class TodoApp:
|
||||
print(f"❌ Error: Todo with ID {todo_id} not found")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error marking todo: {e}") # REQ-009: Error handling
|
||||
print(f"❌ Error marking todo: {e}")
|
||||
return False
|
||||
|
||||
def delete_todo(self, todo_id: int) -> bool:
|
||||
"""
|
||||
Delete a todo item. REQ-004: Delete Todo Items.
|
||||
REQ-009: Error handling.
|
||||
|
||||
Delete a todo item.
|
||||
|
||||
Args:
|
||||
todo_id: The ID of the todo to delete
|
||||
|
||||
@@ -187,14 +167,13 @@ class TodoApp:
|
||||
print(f"❌ Error: Todo with ID {todo_id} not found")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error deleting todo: {e}") # REQ-009: Error handling
|
||||
print(f"❌ Error deleting todo: {e}")
|
||||
return False
|
||||
|
||||
def edit_todo(self, todo_id: int, title: Optional[str] = None,
|
||||
description: Optional[str] = None) -> bool:
|
||||
"""
|
||||
Edit an existing todo item. REQ-005: Edit Todo Items.
|
||||
REQ-007: Input Validation. REQ-009: Error handling.
|
||||
Edit an existing todo item.
|
||||
|
||||
Args:
|
||||
todo_id: The ID of the todo to edit
|
||||
@@ -223,11 +202,11 @@ class TodoApp:
|
||||
print(f"❌ Error: Todo with ID {todo_id} not found")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error editing todo: {e}") # REQ-009: Error handling
|
||||
print(f"❌ Error editing todo: {e}")
|
||||
return False
|
||||
|
||||
def show_menu(self) -> None:
|
||||
"""Display the main menu. REQ-010: CLI user interface."""
|
||||
"""Display the main menu."""
|
||||
print("\n" + "=" * 70)
|
||||
print("📝 Todo Application")
|
||||
print("=" * 70)
|
||||
@@ -240,7 +219,7 @@ class TodoApp:
|
||||
print("=" * 70)
|
||||
|
||||
def run(self) -> None:
|
||||
"""Main application loop. REQ-010: CLI user interface."""
|
||||
"""Main application loop."""
|
||||
print("\n🚀 Welcome to the Simple Todo Application!")
|
||||
|
||||
while True:
|
||||
@@ -248,17 +227,14 @@ class TodoApp:
|
||||
choice = input("Enter your choice (1-6): ").strip()
|
||||
|
||||
if choice == "1":
|
||||
# REQ-001: Create Todo Items
|
||||
title = input("Enter todo title: ").strip()
|
||||
description = input("Enter description (optional): ").strip()
|
||||
self.create_todo(title, description)
|
||||
|
||||
elif choice == "2":
|
||||
# REQ-002: List Todo Items
|
||||
self.list_todos()
|
||||
|
||||
elif choice == "3":
|
||||
# REQ-003: Mark Todo as Complete
|
||||
self.list_todos()
|
||||
try:
|
||||
todo_id = int(input("Enter todo ID to toggle: "))
|
||||
@@ -267,7 +243,6 @@ class TodoApp:
|
||||
print("❌ Error: Please enter a valid ID number")
|
||||
|
||||
elif choice == "4":
|
||||
# REQ-005: Edit Todo Items
|
||||
self.list_todos()
|
||||
try:
|
||||
todo_id = int(input("Enter todo ID to edit: "))
|
||||
@@ -282,7 +257,6 @@ class TodoApp:
|
||||
print("❌ Error: Please enter a valid ID number")
|
||||
|
||||
elif choice == "5":
|
||||
# REQ-004: Delete Todo Items
|
||||
self.list_todos()
|
||||
try:
|
||||
todo_id = int(input("Enter todo ID to delete: "))
|
||||
@@ -293,7 +267,6 @@ class TodoApp:
|
||||
print("❌ Error: Please enter a valid ID number")
|
||||
|
||||
elif choice == "6":
|
||||
# REQ-010: CLI user interface - graceful exit
|
||||
print("\n👋 Goodbye! Your todos have been saved.")
|
||||
break
|
||||
|
||||
|
||||
Reference in New Issue
Block a user