File size: 811 Bytes
48bd848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations


def clamp(value: float, low: float = 0.0, high: float = 100.0) -> float:
    return max(low, min(high, float(value)))


def scale(value, low: float, high: float, invert: bool = False) -> float:
    try:
        number = float(value)
    except Exception:
        return 50.0
    if high == low:
        return 50.0
    score = (number - low) / (high - low) * 100
    score = 100 - score if invert else score
    return clamp(score)


def avg(*values) -> float:
    clean = [float(value) for value in values if value is not None]
    return sum(clean) / len(clean) if clean else 0.0


def safe_float(value, default: float = 0.0) -> float:
    try:
        if value is None:
            return default
        return float(value)
    except Exception:
        return default