-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (33 loc) · 1.34 KB
/
Copy pathmain.py
File metadata and controls
45 lines (33 loc) · 1.34 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
from fastapi import FastAPI
app = FastAPI()
class TemperatureConverter:
@staticmethod
def celsius_to_fahrenheit(celsius: float) -> float:
return (celsius * 9/5) + 32
@staticmethod
def fahrenheit_to_celsius(fahrenheit: float) -> float:
return (fahrenheit - 32) * 5/9
@staticmethod
def celsius_to_kelvin(celsius: float) -> float:
return celsius + 273.15
@staticmethod
def kelvin_to_celsius(kelvin: float) -> float:
return kelvin - 273.15
@staticmethod
def fahrenheit_to_kelvin(fahrenheit: float) -> float:
celsius = TemperatureConverter.fahrenheit_to_celsius(fahrenheit)
return TemperatureConverter.celsius_to_kelvin(celsius)
# Mapping paths to converter functions
conversion_functions = {
"celsius-to-fahrenheit": TemperatureConverter.celsius_to_fahrenheit,
"fahrenheit-to-celsius": TemperatureConverter.fahrenheit_to_celsius,
"celsius-to-kelvin": TemperatureConverter.celsius_to_kelvin,
"kelvin-to-celsius": TemperatureConverter.kelvin_to_celsius,
"fahrenheit-to-kelvin": TemperatureConverter.fahrenheit_to_kelvin,
}
# Dynamic endpoint creation
for route, func in conversion_functions.items():
@app.get(f"/{route}")
async def convert(value: float, func=func):
result = func(value)
return {"input": value, "output": result}