-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
95 lines (73 loc) · 2.65 KB
/
main.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
from datetime import datetime
from socket import *
def main():
global inventory
inventory = open('inventory.txt', 'r') # This is to create the inventory file, only if it doesn't already exist
global inventory_list
inventory_list = []
row = inventory.readlines()
for line in row:
inventory_list.append(line.split())
# send_sorted("date")
# remove("Pear")
# update_quantity("Xbox", 18)
serverSocket = socket(AF_INET, SOCK_STREAM)
webPort = 65432
serverSocket.bind(('10.0.0.1', webPort))
serverSocket.listen(5)
connectionSocket, addr = serverSocket.accept()
while True:
print("connected")
try:
message = connectionSocket.recv(1024).decode()
remote_input = message.split()
print(remote_input)
if (remote_input[0] == '1'):
mode = remote_input[1]
tmp = send_sorted(mode)
tmp = str(tmp)
connectionSocket.sendall(tmp.encode())
elif (remote_input[0] == '2'):
item_to_rem = remote_input[1]
remove(item_to_rem)
elif (remote_input[0] == '3'):
item_to_upd = remote_input[1]
new_num = remote_input[2]
update_quantity(item_to_upd, new_num)
else:
print('invalid remote input')
except IOError:
print("IOError")
connectionSocket.close()
serverSocket.close()
def send_sorted(mode):
if mode == 'name':
return sorted(inventory_list, key=lambda x: x[0])
elif mode == 'quantity':
return sorted(inventory_list, key=lambda x: int(x[1]))
elif mode == 'inventory_date':
return sorted(inventory_list, key=lambda x: datetime.strptime(x[2], '%m-%d-%Y'))
else:
print("Invalid mode specified for sort function")
# print(sorted_inventory)
def remove(name):
# print (inventory_list)
# print('\n')
inventory2 = open('inventory.txt', 'w')
x = [x for x in inventory_list if name in x][0]
inventory_list.pop(inventory_list.index(x))
for element in inventory_list:
inventory2.write(str(' '.join(str(v) for v in element)))
inventory2.write("\n")
inventory2.close()
def update_quantity(name, quantity):
# print (inventory_list)
# print('\n')
inventory2 = open('inventory.txt', 'w')
x = [x for x in inventory_list if name in x][0]
inventory_list[inventory_list.index(x)][1] = quantity
for element in inventory_list:
inventory2.write(str(' '.join(str(v) for v in element)))
inventory2.write("\n")
inventory2.close()
main()