-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_list.py
180 lines (149 loc) · 5.22 KB
/
todo_list.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# python
import csv
import datetime
import os
TODO_FILE = "todo_list.csv"
def load_tasks():
"""Load tasks from the CSV file if it exists."""
tasks = []
# check if the CSV file exists
if os.path.exists(TODO_FILE):
with open(TODO_FILE, mode="r") as file:
reader = csv.DictReader(file)
# read each row from the CSV file and append to the tasks list
for row in reader:
tasks.append(row)
return tasks
def save_tasks(tasks):
"""Save the current list of tasks to the CSV file."""
with open(TODO_FILE, mode="w", newline="") as file:
# define the columns for the CSV file
fieldnames = [
"ID",
"Description",
"Priority",
"Due Date",
"Start Time",
"End Time",
"Status",
]
writer = csv.DictWriter(file, fieldnames=fieldnames)
# write the header and all tasks to the CSV file
writer.writeheader()
writer.writerows(tasks)
def add_task(tasks):
"""Add a new task with user input."""
# assign a unique ID to the new task
task_id = str(len(tasks) + 1)
description = input("Enter task description: ")
priority = input("Enter priority (Low, Medium, High): ").capitalize()
# prompt the user to enter the due date in either format
due_date = input("Enter due date (DD-MM-YYYY or DD/MM/YYYY): ")
if due_date:
# allow both DD-MM-YYYY and DD/MM/YYYY formats by replacing slashes with hyphens
due_date = due_date.replace("/", "-")
try:
# validate the date format
datetime.datetime.strptime(due_date, "%d-%m-%Y")
except ValueError:
print("Invalid date format. Please use DD-MM-YYYY or DD/MM/YYYY.")
return
# prompt the user to enter start and end times in HH:MM format
start_time = input("Enter start time (HH:MM): ")
end_time = input("Enter end time (HH:MM): ")
# validate the start and end times
if not validate_time(start_time) or not validate_time(end_time):
print("Invalid time format. Please use HH:MM.")
return
# new tasks are marked as pending by default
status = "Pending"
# create a dictionary representing the new task
task = {
"ID": task_id,
"Description": description,
"Priority": priority,
"Due Date": due_date,
"Start Time": start_time,
"End Time": end_time,
"Status": status,
}
# add the new task to the tasks list
tasks.append(task)
print("Task added successfully!")
def validate_time(time_str):
"""Validate that a given string is in HH:MM format."""
try:
# use strptime to check if the time is valid
datetime.datetime.strptime(time_str, "%H:%M")
return True
except ValueError:
return False
def list_tasks(tasks):
"""Display all tasks in a table format."""
if not tasks:
print("No tasks found.")
return
# print table headers
print("\nID | Description | Priority | Due Date | Start Time | End Time | Status")
print("-" * 80)
# print each task in a formatted manner
for task in tasks:
print(
f"{task['ID']} | {task['Description']} | {task['Priority']} | {task['Due Date']} | "
f"{task['Start Time']} | {task['End Time']} | {task['Status']}"
)
def mark_task_completed(tasks):
"""Mark a specific task as completed."""
task_id = input("Enter task ID to mark as completed: ")
# find the task by ID and mark it as completed
for task in tasks:
if task["ID"] == task_id:
if task["Status"] == "Completed":
print("Task is already completed.")
else:
task["Status"] = "Completed"
print("Task marked as completed.")
return
print("Task not found.")
def delete_task(tasks):
"""Delete a task by its ID."""
task_id = input("Enter task ID to delete: ")
# search for the task by ID and remove it if found
for i, task in enumerate(tasks):
if task["ID"] == task_id:
tasks.pop(i)
print("Task deleted successfully.")
return
print("Task not found.")
def main():
"""Main function to manage the interactive menu."""
# load existing tasks from the CSV file
tasks = load_tasks()
while True:
# display the menu options
print("\nTo-Do List Manager")
print("1. Add Task")
print("2. List Tasks")
print("3. Mark Task as Completed")
print("4. Delete Task")
print("5. Exit")
# get the user's choice
choice = input("Choose an option (1-5): ")
# call the appropriate function based on the user's choice
if choice == "1":
add_task(tasks)
elif choice == "2":
list_tasks(tasks)
elif choice == "3":
mark_task_completed(tasks)
elif choice == "4":
delete_task(tasks)
elif choice == "5":
# save tasks before exiting
save_tasks(tasks)
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()