Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Task.py #982

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions Task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class TaskError(Exception):
"""Custom exception for task-related errors."""
pass


class Task:
def __init__(self, title, description, priority):
if priority not in ['Low', 'Medium', 'High']:
raise TaskError("Priority must be Low, Medium, or High.")
self.title = title
self.description = description
self.priority = priority
self.completed = False

def complete_task(self):
self.completed = True

def __str__(self):
status = "✔️" if self.completed else "❌"
return f"[{status}] {self.title} (Priority: {self.priority})"


class TaskManager:
def __init__(self):
self.tasks = []

def add_task(self, title, description, priority):
try:
task = Task(title, description, priority)
self.tasks.append(task)
print(f"Task '{title}' added successfully.")
except TaskError as e:
print(f"Error adding task: {e}")

def complete_task(self, title):
for task in self.tasks:
if task.title == title:
task.complete_task()
print(f"Task '{title}' marked as completed.")
return
print(f"Task '{title}' not found.")

def delete_task(self, title):
self.tasks = [task for task in self.tasks if task.title != title]
print(f"Task '{title}' deleted successfully.")

def view_tasks(self):
if not self.tasks:
print("No tasks available.")
return
print("Tasks:")
for task in sorted(self.tasks, key=lambda t: t.priority):
print(task)

def filter_tasks(self, completed=None):
filtered_tasks = self.tasks
if completed is not None:
filtered_tasks = [task for task in self.tasks if task.completed == completed]

if not filtered_tasks:
print("No tasks match the criteria.")
return

print("Filtered Tasks:")
for task in filtered_tasks:
print(task)


# Example usage
if __name__ == "__main__":
manager = TaskManager()
manager.add_task("Write report", "Complete the annual report", "High")
manager.add_task("Email team", "Send out the meeting agenda", "Medium")
manager.add_task("Clean desk", "Organize desk space", "Low")

manager.view_tasks()

manager.complete_task("Write report")
manager.filter_tasks(completed=True)

manager.delete_task("Clean desk")
manager.view_tasks()