-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_bot.py
95 lines (80 loc) · 3.09 KB
/
task_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import logging
import csv
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
filename="task_bot.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
# State transition model for tasks
class TaskState:
CREATED = "Created"
IN_PROGRESS = "In Progress"
COMPLETED = "Completed"
ARCHIVED = "Archived"
class Task:
def __init__(self, task_id, description, priority):
self.task_id = task_id
self.description = description
self.priority = priority
self.state = TaskState.CREATED
def update_state(self, new_state):
if new_state in [TaskState.IN_PROGRESS, TaskState.COMPLETED, TaskState.ARCHIVED]:
self.state = new_state
else:
raise ValueError(f"Invalid state transition: {new_state}")
def __repr__(self):
return f"Task(ID={self.task_id}, Description='{self.description}', Priority='{self.priority}', State='{self.state}')"
class TaskOrganizerBot:
def __init__(self):
self.tasks = []
self.task_counter = 1
def add_task(self, description, priority):
task = Task(self.task_counter, description, priority)
self.tasks.append(task)
self.task_counter += 1
logging.info(f"Task added: {task}")
return task
def update_task_state(self, task_id, new_state):
try:
task = next(t for t in self.tasks if t.task_id == task_id)
task.update_state(new_state)
logging.info(f"Task updated: {task}")
except StopIteration:
logging.error(f"Task ID {task_id} not found.")
raise ValueError("Task ID not found.")
except Exception as e:
logging.error(f"Unexpected error updating task state: {e}")
raise
def generate_report(self, output_file="tasks_report.csv"):
try:
with open(output_file, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Task ID", "Description", "Priority", "State"])
for task in self.tasks:
writer.writerow([task.task_id, task.description, task.priority, task.state])
logging.info("Report generated successfully.")
return output_file
except Exception as e:
logging.error(f"Failed to generate report: {e}")
raise
if __name__ == "__main__":
try:
# Initialize bot
bot = TaskOrganizerBot()
# Add tasks
bot.add_task("Complete Python bot project", "High")
bot.add_task("Prepare presentation slides", "Medium")
bot.add_task("Submit assignment", "High")
# Update task state
bot.update_task_state(1, TaskState.IN_PROGRESS)
bot.update_task_state(1, TaskState.COMPLETED)
# Generate task report
report_file = bot.generate_report()
print(f"Task report generated: {report_file}")
except Exception as main_exception:
logging.critical(f"Unexpected error in TaskOrganizerBot: {main_exception}")