numbers
Full documentation pages are generated for docstring
reference only and may contain symbols imported from other
modules. Imported symbols are not distinguished from locally
defined symbols and will appear in any module that they are
imported into. For better information on where symbols should
be imported from, review the sourcecode on the
github.
FoSpy.parsing.validators.numbers
any_decimal
any_decimal(label, value_key, require_unit=False)
Source code in FoSpy/parsing/validators/numbers.py
| def any_decimal(label, value_key, require_unit=False):
def func(val, sourceDict):
try:
value = Decimal(val)
except:
raise ValueError(f"Unable to convert value: '{val}' into a decimal for '{label}'")
return value
if require_unit:
def unit_func(val, cls, sourceDict):
return attach_unit(func(val), value_key, cls, sourceDict)
return unit_func
return func
|
attach_unit
attach_unit(value, value_key, cls, sourceDict)
Source code in FoSpy/parsing/validators/units.py
| def attach_unit(value, value_key, cls, sourceDict):
unit_key = f"{value_key}_unit"
if unit_key not in sourceDict:
raise ValueError(f"Could not find required unit for: '{value_key}'")
unit = sourceDict[unit_key]
validators = cls.build_validators()
unit_validator = validators.get(unit_key, Unit)
unit = unit_validator(unit)
return FOSQuantity(value, unit)
|
decimal_range
decimal_range(
label,
value_key,
lower=0,
upper=1,
include_lower=False,
include_upper=True,
require_unit=False,
)
Source code in FoSpy/parsing/validators/numbers.py
| def decimal_range(label, value_key, lower=0, upper=1, include_lower=False, include_upper=True, require_unit=False):
def func(val, sourceDict):
try:
value = Decimal(val)
except:
raise ValueError(f"Unable to convert value: '{val}' into a decimal for '{label}'")
if any((
value < lower or value > upper,
value == lower and not include_lower,
value == upper and not include_upper
)):
raise ValueError(f"'{val}' not allowed for '{label}'. Value must be in range: "
f"{lower}{"<=" if include_lower else "<"}"
f"val{"<=" if include_upper else "<"}{lower}")
return value
if require_unit:
def unit_func(val, cls, sourceDict):
return attach_unit(func(val), value_key, cls, sourceDict)
return unit_func
return func
|
positive_decimal
positive_decimal(label, value_key, require_unit=False)
Source code in FoSpy/parsing/validators/numbers.py
| def positive_decimal(label, value_key, require_unit=False):
def func(val):
try:
decimal_val = Decimal(val)
except:
raise ValueError(f"Unable to convert '{label}: {val}' into a decimal number.")
if not decimal_val > 0:
raise ValueError(f"Value for '{label}' must be a positive number.")
return decimal_val
if require_unit:
def unit_func(val, cls, sourceDict):
return attach_unit(func(val), value_key, cls, sourceDict)
return unit_func
return func
|