-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
56 lines (43 loc) · 1.66 KB
/
app.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
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class Home(Resource):
def get(self):
return {'version': '1.0'}
class ConvertTemp(Resource):
def get(self):
# Get arguments from query parameters
temp = float(request.args.get('temp'))
scale = request.args.get('scale').lower()
target_scale = request.args.get('target_scale').lower()
# Perform temperature conversion
converted_temp = self.convert_temperature(temp, scale, target_scale)
if converted_temp is None:
return {'error': 'Invalid scale or target scale'}, 400
return {'converted_temp': converted_temp, 'target_scale': target_scale}
def convert_temperature(self, temp, scale, target_scale):
# Conversion logic
if scale == target_scale:
return temp
if scale == 'celsius':
if target_scale == 'fahrenheit':
return temp * 9/5 + 32
elif target_scale == 'kelvin':
return temp + 273.15
elif scale == 'fahrenheit':
if target_scale == 'celsius':
return (temp - 32) * 5/9
elif target_scale == 'kelvin':
return (temp - 32) * 5/9 + 273.15
elif scale == 'kelvin':
if target_scale == 'celsius':
return temp - 273.15
elif target_scale == 'fahrenheit':
return (temp - 273.15) * 9/5 + 32
# If scales are invalid
return None
api.add_resource(Home, '/')
api.add_resource(ConvertTemp, '/convert-temp')
if __name__ == '__main__':
app.run(debug=True)