-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.py
More file actions
102 lines (83 loc) · 3.18 KB
/
Copy pathtodo.py
File metadata and controls
102 lines (83 loc) · 3.18 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
import argparse
import json
import os
from datetime import datetime
from typing import List, Dict
TODOS_FILE = 'todos.json'
def load_todos() -> List[Dict]:
if os.path.exists(TODOS_FILE):
with open(TODOS_FILE, 'r') as f:
return json.load(f)
return []
def save_todos(todos: List[Dict]):
with open(TODOS_FILE, 'w') as f:
json.dump(todos, f, indent=4)
def add_todo(description: str, priority: str = 'medium'):
todos = load_todos()
todo = {
'id': len(todos) + 1,
'description': description,
'priority': priority.lower(),
'done': False,
'created': datetime.now().isoformat()
}
todos.append(todo)
save_todos(todos)
print(f"Added: {description} (priority: {priority})")
def list_todos():
todos = load_todos()
if not todos:
print("No todos!")
return
for todo in sorted(todos, key=lambda x: {'high': 1, 'medium': 2, 'low': 3}[x['priority']]):
status = "✓" if todo['done'] else "○"
print(f"{status} [{todo['priority'].upper()}] {todo['description']}")
def mark_done(todo_id: int):
todos = load_todos()
for todo in todos:
if todo['id'] == todo_id:
todo['done'] = True
save_todos(todos)
print(f"Marked as done: {todo['description']}")
return
print(f"Todo with id {todo_id} not found.")
def delete_todo(todo_id: int):
todos = load_todos()
todos = [t for t in todos if t['id'] != todo_id]
for i,t in enumerate(todos):t['id'] = i + 1 # Reassign IDs
save_todos(todos)
print(f"Deleted todo with id {todo_id}.")
def search_todos(query: str):
todos = load_todos()
matches = [t for t in todos if query.lower() in t['description'].lower()]
for todo in matches:
status = "✓" if todo['done'] else "○"
print(f"{status} [{todo['priority'].upper()}] {todo['description']}")
# Add more: mark_done(id), delete(id), search(query)...
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Todo CLI")
subparsers = parser.add_subparsers(dest='command')
add_parser = subparsers.add_parser('add', help='Add todo')
add_parser.add_argument('description', help='Todo description')
add_parser.add_argument('--priority', choices=['high', 'medium', 'low'], default='medium')
list_parser = subparsers.add_parser('ls', help='List todos')
done_parser = subparsers.add_parser('done', help='Mark todo as done')
done_parser.add_argument('id', type=int, help='Todo ID')
delete_parser = subparsers.add_parser('del', help='Delete todo')
delete_parser.add_argument('id', type=int, help='Todo ID')
search_parser = subparsers.add_parser('search', help='Search todos')
search_parser.add_argument('query', help='Search query')
# Add done/del/search parsers...
args = parser.parse_args()
if args.command == 'add':
add_todo(args.description, args.priority)
elif args.command == 'ls':
list_todos()
elif args.command == 'done':
mark_done(args.id)
elif args.command == 'del':
delete_todo(args.id)
elif args.command == 'search':
search_todos(args.query)
else:
parser.print_help()