examples.15_enum_with_plain_values
Demonstrates how EnumValue can be constructed from a plain list or
tuple of allowed values (not an Enum class or Enum members).
This example shows:
- How to define a simple allowed value set as a list of primitives.
- How validation passes when the input is in the allowed list.
- How validation fails when the input is not in the allowed list.
Run directly:
python examples/15_enum_with_plain_values.py
Expected output:
ok: OK a bad: EXCEPTION Value must be one of ('a', 'b'), got c
1"""Demonstrates how :class:`EnumValue` can be constructed from a plain list or 2tuple of allowed values (not an Enum class or Enum members). 3 4This example shows: 5 * How to define a simple allowed value set as a list of primitives. 6 * How validation passes when the input is in the allowed list. 7 * How validation fails when the input is not in the allowed list. 8 9Run directly: 10 11 python examples/15_enum_with_plain_values.py 12 13Expected output: 14 15 ok: OK a 16 bad: EXCEPTION Value must be one of ('a', 'b'), got c 17""" 18 19import sys 20import pathlib 21 22# --------------------------------------------------------------------------- 23# Make repo root importable when running this file directly 24# --------------------------------------------------------------------------- 25sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 26 27from constrained_values import EnumValue 28 29 30def main() -> None: 31 """Run the EnumValue plain-value demonstration. 32 33 Creates two :class:`EnumValue` instances with a list of plain string 34 values `["a", "b"]` as the allowed set. 35 36 Steps: 37 1. `EnumValue("a", ["a", "b"])` → passes validation (OK). 38 2. `EnumValue("c", ["a", "b"])` → fails validation (EXCEPTION). 39 40 Prints: 41 * `"ok: OK a"` 42 * `"bad: EXCEPTION Value must be one of ('a', 'b'), got c"` 43 """ 44 ok = EnumValue("a", ["a", "b"]) 45 bad = EnumValue("c", ["a", "b"]) 46 print("ok:", ok.status.name, ok.value) 47 print("bad:", bad.status.name, bad.details) 48 49 50if __name__ == "__main__": 51 main()
def
main() -> None:
31def main() -> None: 32 """Run the EnumValue plain-value demonstration. 33 34 Creates two :class:`EnumValue` instances with a list of plain string 35 values `["a", "b"]` as the allowed set. 36 37 Steps: 38 1. `EnumValue("a", ["a", "b"])` → passes validation (OK). 39 2. `EnumValue("c", ["a", "b"])` → fails validation (EXCEPTION). 40 41 Prints: 42 * `"ok: OK a"` 43 * `"bad: EXCEPTION Value must be one of ('a', 'b'), got c"` 44 """ 45 ok = EnumValue("a", ["a", "b"]) 46 bad = EnumValue("c", ["a", "b"]) 47 print("ok:", ok.status.name, ok.value) 48 print("bad:", bad.status.name, bad.details)
Run the EnumValue plain-value demonstration.
Creates two EnumValue instances with a list of plain string
values ["a", "b"] as the allowed set.
Steps:
EnumValue("a", ["a", "b"])→ passes validation (OK).EnumValue("c", ["a", "b"])→ fails validation (EXCEPTION).
Prints:
"ok: OK a""bad: EXCEPTION Value must be one of ('a', 'b'), got c"