-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtodo.py
More file actions
313 lines (208 loc) · 9.93 KB
/
todo.py
File metadata and controls
313 lines (208 loc) · 9.93 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# this program developed by Mohammad Rasoul Azizi
# Winter 2024
# to do list programm
# Import Libraries
import csv
import os
import sys
# Create class for Tasks with title, priority and done attributes
class Task:
def __init__(self, title, priority='Medium', done=0):
self.title = title
self.priority = priority
self.done = done
# Create TodoList class to manage the to do list program
class TodoList:
def __init__(self):
# variabe to save file and directory
self.file_name = 'todo.csv'
# tasks use for saving the tasks in one list as object from Task class
self.tasks = list()
# call the load_tasks method for load tasks from file to program
self.load_tasks()
# define load_tasks method for load tasks from file to program
def load_tasks(self):
# Checking the existence of the file in the specified path
if os.path.exists(self.file_name):
# open csv file as csvfile
with open(self.file_name) as csvfile:
# read all information from file and save in file_read variable
file_read = csv.reader(csvfile)
# Browse stored information row by row
for row in file_read:
# Checking the length of stored information
if len(row) == 3:
# adding the stored tasks in csvfile as Task's object in tasks variable
self.tasks.append(Task(row[0], row[1], row[2]))
# define the save_tasks for saving tasks from tasks variable into csv file
def save_tasks(self):
# opening the csv file as csvfile
with open(self.file_name, 'w', newline='') as csvfile:
# create the writer object for writing into csv file
writer = csv.writer(csvfile)
# Browse stored tasks in tasks variable row by row
for row in self.tasks:
# write each task in file row by row
writer.writerow([row.title, row.priority, row.done])
# define the create_task for create a new task and save it into csv file
def create_task(self, title, priority='Medium', done=0):
# adding a new Task's object with given parameter into task's variable
self.tasks.append(Task(title, priority, done))
# call the save_tasks method for save new task into csv file
self.save_tasks()
# showing the successfully message for creating task.
print(f'Task \"{title}\" created successfully.')
# define List_tasks for showing the stored tasks in a table
def list_tasks(self):
# Checking the presence of at least one task in the tasks variable.
if self.tasks:
# showing the task as below form
print('Task List:')
print("{:<6} {:<15} {:<10} {:<10}".format('Index', "Title", "Priority", "Done"))
index = 1
for row in self.tasks:
print("{:<6} {:<15} {:<10} {:<10}".format(index, str(row.title), str(row.priority), str(row.done)))
index += 1
# if there is no any task in tasks variable
else:
# showing below message.
print("No tasks found.")
# define update_task for update and change the stored tasks
def update_task(self, title, field, edit):
# open csv file as csvfile
with open(self.file_name) as file:
# read all information in csvfile as list
read = list(csv.reader(file))
# define the flag for checking the specific task is exist or not
flag = False
# brow the tasks row by row
for row in read:
# Checking whether the title of the row is the same as the given title.
if title == row[0]:
# give the True value to flag for existing the title
flag = True
# checking the is field done
if field == 'done':
# setting new value
row[2] = edit
# checking the is field priority
elif field == 'priority':
# setting new value
row[1] = edit
# if title is exist in tasks
if flag:
# opening csv file as file
with open(self.file_name, 'w', newline='') as file:
# create the writer object for writing values into file
writer = csv.writer(file)
# write the updated tasks into file
writer.writerows(read)
# showing the successfull update message
print(f'Task "{title}" updated successfully.')
else:
# if there is no such this title showing the "Invalid title" message
print('Invalid title.')
# define the delete_task method for deleting the specific task from stored tasks
def delete_task(self, title):
# open the csv file as file
with open(self.file_name) as file:
# reading all information from file
read = list(csv.reader(file))
# copying the information into temporary variable
temp = read.copy()
# define the flag for checking the specific task is exist or not
flag = False
# brow the tasks row by row
for row in temp:
# Checking whether the title of the row is the same as the given title.
if title == row[0]:
# give the True value to flag for existing the title
flag = True
# remove the task from stored task
read.remove(row)
# if title is exist in tasks
if flag:
# open csv file as file
with open(self.file_name, 'w', newline='') as file:
# create writer object for writing values into file
writer = csv.writer(file)
# write updated tasks into the file
writer.writerows(read)
# showing the deleted successfull message
print(f'Task "{title}" deleted successfully.')
# if there is no such this title
else:
# showing Invalid title.
print('Invalid title.')
# define the clear_list method for clearing the tasks
def clear_list(self):
# create a empty list
empty_list = list()
# open csv file as file
with open(self.file_name, 'w', newline='') as file:
# create a writer object for writing values into file
writter = csv.writer(file)
# writing the empty list into the file
writter.writerows(empty_list)
# showing the message for user
print('To-do list cleared successfully.')
# defining the get_task method for showing a specific task
def get_task(self, title):
# open csv file as file
with open(self.file_name) as file:
# reading all information from file and save them in read variable
read = list(csv.reader(file))
# brow the tasks row by row
for row in read:
# Checking whether the title of the row is the same as the given title.
if title == row[0]:
# showing the task as specific form for user
print("{:<15} {:<10} {:<10}".format('Title', 'Priority', 'Done'))
print("{:<15} {:<10} {:<10}".format(row[0], row[1], row[2]))
# defining the main function
def main():
# create a object from TodoList class
obj = TodoList()
# getting the arguments that user writes them in terminal
arg_pars_command = sys.argv
# Checking the first word that the user wrote in the terminal commands, is the word "create"?
if arg_pars_command[1] == 'create':
# Checking whether the length of the command entered by the user in the terminal is equal to 5 or not?
if len(arg_pars_command) == 5:
# call create_task method with title, priority, done arguments
obj.create_task(arg_pars_command[2], arg_pars_command[3], arg_pars_command[4])
# Checking whether the length of the command entered by the user in the terminal is equal to 4 or not?
elif len(arg_pars_command) == 4:
# call create_task method with title, priority arguments
obj.create_task(arg_pars_command[2], arg_pars_command[3])
else:
# call create_task method with title argument
obj.create_task(arg_pars_command[2])
# Checking the first word that the user wrote in the terminal commands, is the word "update"?
elif arg_pars_command[1] == 'update':
# calling update_task with title, field, new_value arguments
obj.update_task(arg_pars_command[2], arg_pars_command[3], arg_pars_command[4])
# Checking the first word that the user wrote in the terminal commands, is the word "delete"?
elif arg_pars_command[1] == 'delete':
# calling delete_task with title argument
obj.delete_task(arg_pars_command[2])
# Checking the first word that the user wrote in the terminal commands, is the word "create"?
elif arg_pars_command[1] == 'clear':
# calling the clear_list method
obj.clear_list()
# Checking the first word that the user wrote in the terminal commands, is the word "search"?
elif arg_pars_command[1] == 'search':
# calling get_task with title argument
obj.get_task(arg_pars_command[2])
# Checking the first word that the user wrote in the terminal commands, is the word "list"?
elif arg_pars_command[1] == 'list':
# calling the list method
obj.list_tasks()
# If the entered word is none of the above words:
else:
# showing the "Invalid Command." message.
print("Invalid command.")
# Checking whether the main file is executed?
if __name__ == "__main__":
# calling the main function
main()