examples.12_range_with_coercion
Demonstrates how RangeValue automatically coerces the input type
to match the type of its range bounds.
This example shows:
- When both bounds are
float, anintinput is coerced tofloat.- When both bounds are
Decimal, the input is converted toDecimal.- When both bounds are
Fraction, the input is converted toFraction.
Run directly:
python examples/12_range_with_coercion.py
Expected output:
int->float: 3.0 int->Decimal: 3 int->Fraction: 1
1"""Demonstrates how :class:`RangeValue` automatically coerces the input type 2to match the type of its range bounds. 3 4This example shows: 5 * When both bounds are `float`, an `int` input is coerced to `float`. 6 * When both bounds are `Decimal`, the input is converted to `Decimal`. 7 * When both bounds are `Fraction`, the input is converted to `Fraction`. 8 9Run directly: 10 11 python examples/12_range_with_coercion.py 12 13Expected output: 14 15 int->float: 3.0 16 int->Decimal: 3 17 int->Fraction: 1 18""" 19 20import sys 21import pathlib 22from decimal import Decimal 23from fractions import Fraction 24 25# --------------------------------------------------------------------------- 26# Make repo root importable when running this file directly 27# --------------------------------------------------------------------------- 28sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 29 30from constrained_values import RangeValue 31 32 33def main() -> None: 34 """Run the range coercion demonstration. 35 36 Creates three :class:`RangeValue` instances with different numeric bound 37 types to demonstrate automatic coercion of the input value. 38 39 Steps: 40 1. Input `3` coerced to `float` because bounds are `0.0` and `10.0`. 41 2. Input `3` coerced to `Decimal` because bounds are Decimal objects. 42 3. Input `1` coerced to `Fraction` because bounds are Fractions. 43 44 Prints: 45 * `"int->float: 3.0"` 46 * `"int->Decimal: 3"` 47 * `"int->Fraction: 1"` 48 """ 49 print("int->float:", RangeValue(3, 0.0, 10.0).value) 50 print("int->Decimal:", RangeValue(3, Decimal("0"), Decimal("10")).value) 51 print("int->Fraction:", RangeValue(1, Fraction(0, 1), Fraction(3, 2)).value) 52 53 54if __name__ == "__main__": 55 main()
def
main() -> None:
34def main() -> None: 35 """Run the range coercion demonstration. 36 37 Creates three :class:`RangeValue` instances with different numeric bound 38 types to demonstrate automatic coercion of the input value. 39 40 Steps: 41 1. Input `3` coerced to `float` because bounds are `0.0` and `10.0`. 42 2. Input `3` coerced to `Decimal` because bounds are Decimal objects. 43 3. Input `1` coerced to `Fraction` because bounds are Fractions. 44 45 Prints: 46 * `"int->float: 3.0"` 47 * `"int->Decimal: 3"` 48 * `"int->Fraction: 1"` 49 """ 50 print("int->float:", RangeValue(3, 0.0, 10.0).value) 51 print("int->Decimal:", RangeValue(3, Decimal("0"), Decimal("10")).value) 52 print("int->Fraction:", RangeValue(1, Fraction(0, 1), Fraction(3, 2)).value)
Run the range coercion demonstration.
Creates three RangeValue instances with different numeric bound
types to demonstrate automatic coercion of the input value.
Steps:
- Input
3coerced tofloatbecause bounds are0.0and10.0.- Input
3coerced toDecimalbecause bounds are Decimal objects.- Input
1coerced toFractionbecause bounds are Fractions.
Prints:
"int->float: 3.0""int->Decimal: 3""int->Fraction: 1"