-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path463.py
190 lines (147 loc) · 5.85 KB
/
463.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from flask import Flask, render_template, request, jsonify
import pandas as pd
import openpyxl
from openpyxl import Workbook
import itertools
from copy import deepcopy
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/handle_data', methods=['POST'])
def handle_data():
formData2 = request.form.getlist('city[]')
formData=[]
for i in formData2:
formData.append( i.strip() )
'''read hotel data'''
path = "static/hotel.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
temp = pd.DataFrame(sheet_obj.values)
headers = temp.iloc[0]
hotel = pd.DataFrame(temp.values[1:], columns=headers)
'''read flight data'''
path = "static/flight.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
temp = pd.DataFrame(sheet_obj.values)
headers = temp.iloc[0]
flight = pd.DataFrame(temp.values[1:], columns=headers)
'''declare variables'''
allH={}
allP={}
startCity=formData[0]
startDay=int(formData[1].split("-")[2])
route={}
#fill the route with the data form POST request
for i in range(2,len(formData),2):
route[ formData[i] ] = int(formData[i+1])
'''calculate hotel prices'''
permutations=itertools.permutations(route)
for permutation in permutations:
hotel_cost=0
day=startDay
for city in permutation:
for i in range(route[city]):
try:
row=hotel.loc[hotel['city'] == city].loc[hotel['startDay'] == day].head(1)
price= int(row["price"])
hotel_cost+=price
#print(city,day,price)
day+=1
except Exception as e:
print("Hotel Error1: ",e)
#print(permutation,hotel_cost)
allH[permutation]=hotel_cost
#print()
'''Calculate flight prices'''
permutations=itertools.permutations(route)
for permutation in permutations:
flight_cost=0
day=startDay
permutationCopy=deepcopy(permutation)
#print("*",permutation)
permutation=list(permutation)
permutation.insert(0,startCity)
permutation.append(startCity)
for i in range(0,len(permutation)-1):
try:
if permutation[i]==startCity:
day+=0
else:
day+=route[permutation[i]]
row=flight.loc[flight['Departure'] == permutation[i]].loc[flight['Arrival'] == permutation[i+1]]
row=row.loc[flight['date']==day].head(1)
price= int(row["price"])
flight_cost+=price
except Exception as e:
price= 9999
flight_cost+=price
print("Flight Error1: ",e)
allP[permutationCopy]=flight_cost
'''Sum costs and order'''
permutations=itertools.permutations(route)
l=[]
for permutation in permutations:
p1=allH[permutation]
p2=allP[permutation]
l.append([p1+p2,permutation,p1,p2])
l.sort(key=lambda x: x[0])
def trace(l,i,j):
result_permutation=l[i][j]
resultJSON={}
for cityName in result_permutation:
resultJSON[cityName] = {"day":0,"hotel_cost":0,"flight_cost":0}
resultJSON[startCity] = {"day":0,"hotel_cost":0,"flight_cost":0}
day=startDay
for city in result_permutation:
hotel_city_cost=0
row=hotel.loc[hotel['city'] == city].loc[hotel['startDay'] == day].head(1)
hotelName = row["hotelName"].to_string().split()[1:]
hotelName = " ".join(hotelName)
#print("HOTELHHOTELHOTEL",hotelName)
for i in range(route[city]):
try:
row=hotel.loc[hotel['city'] == city].loc[hotel['startDay'] == day].head(1)
price= int(row["price"])
hotel_city_cost+=price
#print(city,day,price)
day+=1
except Exception as e:
print(e)
resultJSON[city]["hotel_cost"] = hotel_city_cost
resultJSON[city]["hotelName"] = hotelName
result_permutation=list(result_permutation)
result_permutation.insert(0,startCity)
result_permutation.append(startCity)
day=startDay
for i in range(0,len(result_permutation)-1):
try:
if result_permutation[i]==startCity:
day+=0
else:
day+=route[result_permutation[i]]
row=flight.loc[flight['Departure'] == result_permutation[i]].loc[flight['Arrival'] == result_permutation[i+1]]
row=row.loc[flight['date']==day].head(1)
price= int(row["price"])
departureTime= row["departureTime"].to_string().split()[1]
arrivalTime = row["arrivalTime"].to_string().split()[1]
company = row["company"].to_string().split()[1:]
#print("******",company,type(company))
company = " ".join(company)
#print("******",company,type(company))
resultJSON[result_permutation[i+1]]["day"] = day
resultJSON[result_permutation[i+1]]["flight_cost"] = price
resultJSON[result_permutation[i+1]]["departureTime"] = departureTime
resultJSON[result_permutation[i+1]]["arrivalTime"] = arrivalTime
resultJSON[result_permutation[i+1]]["company"] = company
except Exception as e:
print("Flight Error2: ",e)
return resultJSON
toReturn= { "Best": trace(l,0,1) , "Second": trace(l,1,1) }
if len(l)>2:
toReturn["Third"] = trace(l,2,1)
return jsonify(toReturn)
if __name__ == '__main__':
app.run(debug = True)