forked from ajupazhamayil/TSP_In_GMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserServer.py
More file actions
71 lines (56 loc) · 1.82 KB
/
parserServer.py
File metadata and controls
71 lines (56 loc) · 1.82 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
from flask import Flask, request, jsonify, render_template
import json
import subprocess as sp
import os
def replace_word(infile,old_word,new_word):
if not os.path.isfile(infile):
print ("Error on replace_word, not a regular file: "+infile)
sys.exit(1)
f1=open(infile,'r').read()
f2=open(infile,'w')
m=f1.replace(old_word,new_word)
f2.write(m)
app = Flask(__name__)
@app.route('/', strict_slashes=False)
def index():
return render_template("index.html")
@app.route('/solution', methods=['GET', 'POST'], strict_slashes=False)
def json_parser():
mapToPoint={}
pointToMap = {}
content = request.json
num_of_points = content["size"]
print content
replace_word("ABC_TSP.cpp","#define D 4","#define D "+str(num_of_points))
print "replaced #define"
flag = False
uniqValue=0
#make the map for points
for i in content['data']:
if (mapToPoint.get(str(i['s']), None)==None):
mapToPoint[str(i['s'])] = str(uniqValue)
pointToMap[str(uniqValue)]=str(i['s'])
uniqValue=uniqValue+1
print mapToPoint
print pointToMap
with open('file.json', 'w') as f:
for i in content['data']:
temp = ""
if flag==True:
temp=temp+"\n"
flag = True
temp=temp+mapToPoint[str(i['s'])]+' '+mapToPoint[str(i['d'])]+' '+str(i['w'])
f.write(temp)
sp.call(["g++","ABC_TSP.cpp"])
sp.call("./a.out")
print "completed code running"
replace_word("ABC_TSP.cpp","#define D "+str(num_of_points),"#define D 4")
#print("Called the cpp function")
with open('fileout.json', 'r') as f:
result = f.readline()
temp=""
for r in result.split(" "):
temp=temp+str(pointToMap[r])+" "
return jsonify({"result":temp})
if __name__ == '__main__':
app.run(host= '127.0.0.1',port=5000,debug=True)