Skip to content

bagof.converters.numbers

Converters for numeric types.

Classes

ToNumber

ToNumber(hint: Any = UNSET, compose: bool = False)

Bases: Converter[NUMBER, Any]

Converter for numbers.Number types.

Example

>>> from bagof.converters import get_converter
>>> get_converter(int)("42")
42
>>> get_converter(float)("1.5")
1.5

Methods:

like
like(__reentrant: tuple = ()) -> Any

Return the input hint for this converter.

__call__
__call__(value: Any) -> NUMBER

Convert the value to a number.

ToBool

ToBool(hint: Any = UNSET, compose: bool = False)

Bases: ToNumber[bool]

Converter for bool.

Warning

Strings are parsed by spelling rather than by truthiness -- "false", "0", "no" and the like become False, not True (which is what plain bool does with any non-empty string). Unrecognised strings raise, rather than defaulting to True.

Methods:

like
like(__reentrant: tuple = ()) -> Any

Accept booleans, integers and the recognised string spellings.

__call__
__call__(value: Any) -> bool

Convert the value to a bool.

ToPositive

ToPositive(hint: Any = UNSET, compose: bool = False)

Bases: ToNumber[NUMBER]

Converter that also checks the value is positive (> 0).

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert the value to a number and check it is positive.

ToNegative

ToNegative(hint: Any = UNSET, compose: bool = False)

Bases: ToNumber[NUMBER]

Converter that also checks the value is negative (< 0).

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert the value to a number and check it is negative.

ToNonNegative

ToNonNegative(hint: Any = UNSET, compose: bool = False)

Bases: ToNumber[NUMBER]

Converter that also checks the value is non-negative (>= 0).

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert the value to a number and check it is non-negative.

ToNonPositive

ToNonPositive(hint: Any = UNSET, compose: bool = False)

Bases: ToNumber[NUMBER]

Converter that also checks the value is non-positive (<= 0).

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert the value to a number and check it is non-positive.

ToLessThan

ToLessThan(threshold: NUMBER, hint: Any = UNSET)

Bases: _ComparatorConverter[NUMBER]

Converter that also checks the value is less than a threshold.

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert and check value < threshold.

ToLessEqual

ToLessEqual(threshold: NUMBER, hint: Any = UNSET)

Bases: _ComparatorConverter[NUMBER]

Converter that also checks the value is at most a threshold.

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert and check value <= threshold.

ToGreaterThan

ToGreaterThan(threshold: NUMBER, hint: Any = UNSET)

Bases: _ComparatorConverter[NUMBER]

Converter that also checks the value is greater than a threshold.

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert and check value > threshold.

ToGreaterEqual

ToGreaterEqual(threshold: NUMBER, hint: Any = UNSET)

Bases: _ComparatorConverter[NUMBER]

Converter that also checks the value is at least a threshold.

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert and check value >= threshold.

ToInRange

ToInRange(min_value: NUMBER, max_value: NUMBER, inclusive: bool | tuple[bool, bool] = True, hint: Any = UNSET)

Bases: ToNumber[NUMBER]

Converter that also checks the value is within a range.

Parameters:

Name Type Description Default
min_value NUMBER

The minimum value of the range.

required
max_value NUMBER

The maximum value of the range.

required
inclusive bool | (bool, bool)

Whether the range is inclusive on both ends. If a single boolean is provided, it applies to both ends.

True
hint Any

The type hint to convert to.

UNSET

Methods:

__call__
__call__(value: Any) -> NUMBER

Convert and check the value is in [min_value, max_value].