examples.14_enum_with_members_sequence
Demonstrates how EnumValue can validate input against a sequence
of allowed Enum members instead of an Enum class.
This example shows:
- How to supply a sequence of specific Enum members as valid options.
- How input may be an Enum member or its underlying primitive value.
- The
statusreportsStatus.OKwhen the input matches any allowed member.
Run directly:
python examples/14_enum_with_members_sequence.py
Expected output:
x: OK a y: OK b
1"""Demonstrates how :class:`EnumValue` can validate input against a sequence 2of allowed Enum members instead of an Enum class. 3 4This example shows: 5 * How to supply a sequence of specific Enum members as valid options. 6 * How input may be an Enum member or its underlying primitive value. 7 * The `status` reports `Status.OK` when the input matches any allowed member. 8 9Run directly: 10 11 python examples/14_enum_with_members_sequence.py 12 13Expected output: 14 15 x: OK a 16 y: OK b 17""" 18 19import sys 20import pathlib 21from enum import Enum 22 23# --------------------------------------------------------------------------- 24# Make repo root importable when running this file directly 25# --------------------------------------------------------------------------- 26sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 27 28from constrained_values import EnumValue 29 30 31class Mixed(Enum): 32 """A simple mixed-type enumeration.""" 33 A = "a" 34 B = "b" 35 36 37def main() -> None: 38 """Run the EnumValue sequence demonstration. 39 40 Creates two :class:`EnumValue` instances validated against a predefined 41 list of allowed Enum members `[Mixed.A, Mixed.B]`. 42 43 Steps: 44 1. `EnumValue(Mixed.A, allowed)` — uses a valid Enum member directly. 45 2. `EnumValue("b", allowed)` — uses the underlying value of an Enum 46 member and still resolves successfully. 47 48 Prints: 49 * `"x: OK a"` 50 * `"y: OK b"` 51 """ 52 allowed = [Mixed.A, Mixed.B] 53 x = EnumValue(Mixed.A, allowed) 54 y = EnumValue("b", allowed) 55 print("x:", x.status.name, x.value) 56 print("y:", y.status.name, y.value) 57 58 59if __name__ == "__main__": 60 main()
class
Mixed(enum.Enum):
A simple mixed-type enumeration.
A =
<Mixed.A: 'a'>
B =
<Mixed.B: 'b'>
Inherited Members
- enum.Enum
- name
- value
def
main() -> None:
38def main() -> None: 39 """Run the EnumValue sequence demonstration. 40 41 Creates two :class:`EnumValue` instances validated against a predefined 42 list of allowed Enum members `[Mixed.A, Mixed.B]`. 43 44 Steps: 45 1. `EnumValue(Mixed.A, allowed)` — uses a valid Enum member directly. 46 2. `EnumValue("b", allowed)` — uses the underlying value of an Enum 47 member and still resolves successfully. 48 49 Prints: 50 * `"x: OK a"` 51 * `"y: OK b"` 52 """ 53 allowed = [Mixed.A, Mixed.B] 54 x = EnumValue(Mixed.A, allowed) 55 y = EnumValue("b", allowed) 56 print("x:", x.status.name, x.value) 57 print("y:", y.status.name, y.value)
Run the EnumValue sequence demonstration.
Creates two EnumValue instances validated against a predefined
list of allowed Enum members [Mixed.A, Mixed.B].
Steps:
EnumValue(Mixed.A, allowed)— uses a valid Enum member directly.EnumValue("b", allowed)— uses the underlying value of an Enum member and still resolves successfully.
Prints:
"x: OK a""y: OK b"