-
Notifications
You must be signed in to change notification settings - Fork 11
/
solve_me.py
117 lines (98 loc) · 3.74 KB
/
solve_me.py
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
from http.server import BaseHTTPRequestHandler, HTTPServer
class TasksCommand:
TASKS_FILE = "tasks.txt"
COMPLETED_TASKS_FILE = "completed.txt"
current_items = {}
completed_items = []
def read_current(self):
try:
file = open(self.TASKS_FILE, "r")
for line in file.readlines():
item = line[:-1].split(" ")
self.current_items[int(item[0])] = " ".join(item[1:])
file.close()
except Exception:
pass
def read_completed(self):
try:
file = open(self.COMPLETED_TASKS_FILE, "r")
self.completed_items = file.readlines()
file.close()
except Exception:
pass
def write_current(self):
with open(self.TASKS_FILE, "w+") as f:
f.truncate(0)
for key in sorted(self.current_items.keys()):
f.write(f"{key} {self.current_items[key]}\n")
def write_completed(self):
with open(self.COMPLETED_TASKS_FILE, "w+") as f:
f.truncate(0)
for item in self.completed_items:
f.write(f"{item}\n")
def runserver(self):
address = "127.0.0.1"
port = 8000
server_address = (address, port)
httpd = HTTPServer(server_address, TasksServer)
print(f"Started HTTP Server on http://{address}:{port}")
httpd.serve_forever()
def run(self, command, args):
self.read_current()
self.read_completed()
if command == "add":
self.add(args)
elif command == "done":
self.done(args)
elif command == "delete":
self.delete(args)
elif command == "ls":
self.ls()
elif command == "report":
self.report()
elif command == "runserver":
self.runserver()
elif command == "help":
self.help()
def help(self):
print(
"""Usage :-
$ python tasks.py add 2 hello world # Add a new item with priority 2 and text "hello world" to the list
$ python tasks.py ls # Show incomplete priority list items sorted by priority in ascending order
$ python tasks.py del PRIORITY_NUMBER # Delete the incomplete item with the given priority number
$ python tasks.py done PRIORITY_NUMBER # Mark the incomplete item with the given PRIORITY_NUMBER as complete
$ python tasks.py help # Show usage
$ python tasks.py report # Statistics
$ python tasks.py runserver # Starts the tasks management server"""
)
def add(self, args):
pass # Use your existing implementation
def done(self, args):
pass # Use your existing implementation
def delete(self, args):
pass # Use your existing implementation
def ls(self):
pass # Use your existing implementation
def report(self):
pass # Use your existing implementation
def render_pending_tasks(self):
# Complete this method to return all incomplete tasks as HTML
return "<h1> Show Incomplete Tasks Here </h1>"
def render_completed_tasks(self):
# Complete this method to return all completed tasks as HTML
return "<h1> Show Completed Tasks Here </h1>"
class TasksServer(TasksCommand, BaseHTTPRequestHandler):
def do_GET(self):
task_command_object = TasksCommand()
if self.path == "/tasks":
content = task_command_object.render_pending_tasks()
elif self.path == "/completed":
content = task_command_object.render_completed_tasks()
else:
self.send_response(404)
self.end_headers()
return
self.send_response(200)
self.send_header("content-type", "text/html")
self.end_headers()
self.wfile.write(content.encode())