forked from longtao.wu/droboticflow
61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import math
|
|
from collections.abc import Iterable
|
|
|
|
|
|
def _parse_finite_float(label: str, value: str) -> tuple[float | None, str | None]:
|
|
if not value:
|
|
return None, f"{label} is required"
|
|
try:
|
|
parsed_value = float(value)
|
|
except ValueError:
|
|
return None, f"{label} must be numeric"
|
|
if not math.isfinite(parsed_value):
|
|
return None, f"{label} must be finite"
|
|
return parsed_value, None
|
|
|
|
|
|
def unit_interval_threshold_issues(thresholds: Iterable[tuple[str, str]]) -> list[str]:
|
|
issues: list[str] = []
|
|
for label, value in thresholds:
|
|
parsed_value, issue = _parse_finite_float(label, value)
|
|
if issue:
|
|
issues.append(issue)
|
|
continue
|
|
if parsed_value is None:
|
|
continue
|
|
if parsed_value < 0 or parsed_value > 1:
|
|
issues.append(f"{label} must be in the range [0, 1]")
|
|
return issues
|
|
|
|
|
|
def positive_float_issues(values: Iterable[tuple[str, str]]) -> list[str]:
|
|
issues: list[str] = []
|
|
for label, value in values:
|
|
parsed_value, issue = _parse_finite_float(label, value)
|
|
if issue:
|
|
issues.append(issue)
|
|
continue
|
|
if parsed_value is None:
|
|
continue
|
|
if parsed_value <= 0:
|
|
issues.append(f"{label} must be greater than 0")
|
|
return issues
|
|
|
|
|
|
def positive_int_issues(values: Iterable[tuple[str, str]]) -> list[str]:
|
|
issues: list[str] = []
|
|
for label, value in values:
|
|
if not value:
|
|
issues.append(f"{label} is required")
|
|
continue
|
|
try:
|
|
parsed_value = int(str(value).strip())
|
|
except ValueError:
|
|
issues.append(f"{label} must be an integer")
|
|
continue
|
|
if parsed_value <= 0:
|
|
issues.append(f"{label} must be greater than 0")
|
|
return issues
|