-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.py
More file actions
111 lines (98 loc) · 3.08 KB
/
Copy pathtask1.py
File metadata and controls
111 lines (98 loc) · 3.08 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
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():
tasks=load_tasks()
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']='done'
task['updatedAt']=datetime.now().isoformat()
found=True
break
if found:
save_tasks(tasks)
print(f'Task {task_id} markes 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 succesfully')
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'no task found for {keyword}')
else:
for task in found:
print(f"id:{task['id']},description{task['description']},status:{task['status']},updatedAt:{task['updatedAt']}")
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:
add_tasks(args[2])
elif args[1]=='done':
if len(args)<3:
print('usage:python3 task.py done task_id')
else:
try:
task_id=int(args[2])
mark_task_done(task_id)
except ValueError:
print('task id mus 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]=='list':
list_tasks()