-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
187 lines (157 loc) · 5.22 KB
/
Copy pathtask.py
File metadata and controls
187 lines (157 loc) · 5.22 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
import sys
import json
import os
from datetime import datetime
TASKS_FILE='tasks.json'
def load_tasks():
if not os.path.exists(TASKS_FILE):
return []
with open(TASKS_FILE,'r') as f:
return json.load(f)
def save_tasks(tasks):
with open(TASKS_FILE,'w') as f:
json.dump(tasks,f,indent=4)
def add_tasks(description):
tasks=load_tasks()
task_id=tasks[-1]['id']+1 if tasks else 1
now=datetime.now().isoformat()
task={
'id':task_id,
'description':description,
'status':'todo',
'createdAt':now,
'updatedAt':now
}
tasks.append(task)
save_tasks(tasks)
print(f'Task addded succesfully (ID:{task_id})')
def list_tasks(status=None):
tasks=load_tasks()
if status:
tasks=[task for task in tasks if task['status']==status]
if not tasks:
print('no tasks found')
return
for task in tasks:
print(f"id:{task['id']},description:{task['description']},status:{task['status']},createdAt:{task['createdAt']},updatedAt:{task['updatedAt']}")
def mark_task_done(task_id):
tasks=load_tasks()
found=False
for task in tasks:
if task['id']==task_id:
task['status']='mark-done'
task['updatedAt']=datetime.now().isoformat()
found=True
break
if found:
save_tasks(tasks)
print(f'Task ID {task_id} marked as done')
else:
print(f'Task ID {task_id} not found')
def delete_task(task_id):
tasks=load_tasks()
new_tasks=[task for task in tasks if task['id']!=task_id ]
if len(new_tasks)==len(tasks):
print(f'task id {task_id} not found')
else:
save_tasks(new_tasks)
print(f'task id {task_id } deleted succesfullly')
def search_tasks(keyword):
tasks=load_tasks()
found=[task for task in tasks if keyword.lower() in task['description'].lower()]
if not found:
print(f'not found for {keyword}')
else:
for task in found:
print(f"id:{task['id']},description:{task['description']},status:{task['status']},createdAt:{task['createdAt']},updatedAt:{task['updatedAt']}")
def update_task(task_id,new_description):
tasks=load_tasks()
found=False
for task in tasks:
if task['id']==task_id:
task['description']=new_description
task['updatedAt']=datetime.now().isoformat()
found=True
break
if found:
save_tasks(tasks)
print(f'task {task_id} updated succesfully')
else:
print(f'task id {task_id} not found')
def mark_task_in_progress(task_id):
tasks=load_tasks()
found=False
for task in tasks:
if task['id']==task_id:
task['status']='in progress'
task['updatedAt']=datetime.now().isoformat()
found=True
break
if found:
save_tasks(tasks)
print(f'task id {task_id} marked as in progress')
else:
print(f'task id {task_id } not found')
if __name__== '__main__':
args=sys.argv
if len(args)<2 :
print('usage: command [arguments]')
elif args[1]=='add':
if len(args)<3:
print('usage: python3 task.py add your description')
else:
description=' '.join(args[2:])
add_tasks(description)
elif args[1]=='mark-done':
if len(args)<3:
print('usage:python3 task.py mark-done task_id')
else:
try:
task_id=int(args[2])
mark_task_done(task_id)
except ValueError:
print('task id must be a number')
elif args[1]=='delete':
if len(args)<3:
print('usage:python3 task.py delete task_id')
else:
try:
task_id=int(args[2])
delete_task(task_id)
except ValueError:
print('task id must be a number')
elif args[1]=='search':
if len(args)<3:
print('usage:python3 task.py search keyword')
else:
search_tasks(args[2])
elif args[1]=='update':
if len(args)<4:
print('usage:python3 task.py update task_id description')
else:
try:
task_id=int(args[2])
new_description=' '.join(args[3:])
update_task(task_id,new_description)
except ValueError:
print('task id must be a number')
elif args[1]=='mark-in-progress':
if len(args)<3:
print('usage: python3 task.py mark-in-progress task_id')
else:
try:
task_id=int(args[2])
mark_task_in_progress(task_id)
except ValueError:
print('task id must be a number')
elif args[1]=='list':
if len(args)>=3:
status = ' '.join(args[2:]).lower()
if status in ['todo','mark-done','mark-in-progress']:
list_tasks(status)
else:
print('invlaid key.use todo mark-done or mark-in-progress')
else:
list_tasks()
else:
print(f'unknown command {args[1]}')