EyeTrackVR/EyeTrackApp/settings/modules/CommonFieldValidators.py
2025-02-08 18:18:50 +13:00

43 lines
1.1 KiB
Python

import ipaddress
def check_is_float_convertible(v: str):
"""
Check if value provided can be converted to a float or double.
PySimpleGUI does not support floats or doubles in UI, so we have to make sure
that what the user typed in is correct
"""
try:
float(v)
return v
except ValueError:
raise ValueError("Please provide a proper number")
def try_convert_to_float(v: str):
"""
Check if value provided can be converted to a float and return converted result
Basically what `check_is_float_convertible` does but returns a float
"""
try:
return float(check_is_float_convertible(v))
except ValueError:
raise
def check_is_ip_address(v: str):
try:
ipaddress.IPv4Address(v)
return v
except ValueError:
raise ValueError("Please provide a valid IP Address")
def try_convert_to_int(v: str):
""""
Checks if value provided can be converted to an integer and returns the converted result
"""
try:
return int(v)
except ValueError:
raise ValueError("Please provide a number with no decimal points")