forked from rtk-rnjn/Diabetes-Checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
96 lines (71 loc) · 2.62 KB
/
utils.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
from __future__ import annotations
import asyncio
from concurrent.futures import ThreadPoolExecutor
from functools import partial, wraps
from typing import TYPE_CHECKING, Any, Callable, Literal, TypedDict
if TYPE_CHECKING:
from quart import Request
class _MissingSentinel:
__slots__ = ()
def __eq__(self, other) -> bool:
return False
def __bool__(self) -> bool:
return False
def __hash__(self) -> int:
return 0
def __repr__(self):
return "..."
MISSING: Any = _MissingSentinel()
class Data(TypedDict):
age: int
bmi: float
hb1ac_level: float
blood_glucose_level: float
heart_disease: int
gender: Literal["Male", "Female"]
hypertension: int
smoking_history: Literal["never", "former", "current"]
def converter(
entity: str | None, check: Callable[..., bool], func: Callable[..., Any]
) -> Any:
if not entity:
return None
if check(entity):
return func(entity)
def get_data(req: Request) -> Data:
age = converter(req.args.get("age"), lambda x: x.isdigit(), int)
weight = converter(req.args.get("weight"), lambda x: x.isdigit(), float)
height = converter(req.args.get("height"), lambda x: x.isdigit(), float)
hb1ac_level = converter(req.args.get("hb1acLevel"), lambda x: x.isdigit(), float)
heart_disease = converter(req.args.get("heartDisease"), lambda x: x.isdigit(), int)
blood_glucose_level = converter(
req.args.get("bloodGlucoseLevel"), lambda x: x.isdigit(), float
)
gender = converter(req.args.get("gender"), lambda x: x in ("Male", "Female"), str)
hypertension = converter(req.args.get("hypertension"), lambda x: x.isdigit(), int)
smoking = converter(
req.args.get("smoking"), lambda x: x in ("never", "former", "current"), str
)
bmi = weight / ((height / 100) ** 2)
return {
"age": age,
"bmi": bmi,
"hb1ac_level": hb1ac_level,
"blood_glucose_level": blood_glucose_level,
"heart_disease": heart_disease,
"gender": gender,
"hypertension": hypertension,
"smoking_history": smoking,
}
class ToAsync:
def __init__(self, *, executor: ThreadPoolExecutor | None = None) -> None:
self.executor = executor
def __call__(self, blocking) -> Callable:
@wraps(blocking)
async def wrapper(*args, **kwargs) -> Any:
loop = asyncio.get_event_loop()
if not self.executor:
self.executor = ThreadPoolExecutor()
func = partial(blocking, *args, **kwargs)
return await loop.run_in_executor(self.executor, func)
return wrapper