-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
123 lines (95 loc) · 2.93 KB
/
Copy pathapp.py
File metadata and controls
123 lines (95 loc) · 2.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
import sqlite3
import sys
from datetime import datetime
DB_FILE = "tasks.db"
def get_connection():
conn = sqlite3.connect(DB_FILE)
conn.execute("""
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
done INTEGER NOT NULL DEFAULT 0,
created TEXT NOT NULL
)
""")
conn.commit()
return conn
def add_task(title):
conn = get_connection()
now = datetime.now().strftime("%Y-%m-%d %H:%M")
conn.execute(
"INSERT INTO tasks (title, done, created) VALUES (?, 0, ?)",
(title, now)
)
conn.commit()
conn.close()
print(f'Added task: "{title}"')
def list_tasks():
conn = get_connection()
rows = conn.execute("SELECT id, title, done, created FROM tasks ORDER BY id").fetchall()
conn.close()
if not rows:
print("No tasks yet.")
return
print("\nTask list:")
print("-" * 50)
for row in rows:
id_, title, done, created = row
status = "[x]" if done else "[ ]"
print(f" [{id_}] {status} {title} ({created})")
print("-" * 50)
print()
def complete_task(task_id):
conn = get_connection()
cursor = conn.execute("UPDATE tasks SET done = 1 WHERE id = ?", (task_id,))
conn.commit()
conn.close()
if cursor.rowcount == 0:
print(f"Task #{task_id} was not found.")
else:
print(f"Marked task #{task_id} as done.")
def delete_task(task_id):
conn = get_connection()
cursor = conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
conn.commit()
conn.close()
if cursor.rowcount == 0:
print(f"Task #{task_id} was not found.")
else:
print(f"Deleted task #{task_id}.")
def show_help():
print("""
Task Tracker - usage:
python3 app.py add "Task title" Add a new task
python3 app.py list Show all tasks
python3 app.py done <id> Mark a task as done
python3 app.py delete <id> Delete a task
python3 app.py help Show this help message
""")
def main():
args = sys.argv[1:]
if not args or args[0] == "help":
show_help()
elif args[0] == "add":
if len(args) < 2:
print('Usage: python3 app.py add "Task title"')
else:
# Join all remaining words so the task title can contain spaces.
add_task(" ".join(args[1:]))
elif args[0] == "list":
list_tasks()
elif args[0] == "done":
if len(args) < 2 or not args[1].isdigit():
print("Usage: python3 app.py done <id>")
else:
complete_task(int(args[1]))
elif args[0] == "delete":
if len(args) < 2 or not args[1].isdigit():
print("Usage: python3 app.py delete <id>")
else:
delete_task(int(args[1]))
else:
print(f'Unknown command: "{args[0]}"')
show_help()
if __name__ == "__main__":
main()