examples.13_enum_with_class
Demonstrates how EnumValue handles inputs that are either Enum
members or their underlying primitive values.
This example shows:
- How
EnumValueaccepts both an Enum member and its raw value.- The resulting canonical value is always the Enum member.
- The
statusindicates successful validation in both cases.
Run directly:
python examples/13_enum_with_class.py
Expected output:
member → OK True underlying → OK True
1"""Demonstrates how :class:`EnumValue` handles inputs that are either Enum 2members or their underlying primitive values. 3 4This example shows: 5 * How `EnumValue` accepts both an Enum member and its raw value. 6 * The resulting canonical value is always the Enum member. 7 * The `status` indicates successful validation in both cases. 8 9Run directly: 10 11 python examples/13_enum_with_class.py 12 13Expected output: 14 15 member → OK True 16 underlying → OK True 17""" 18 19from enum import Enum 20from constrained_values import EnumValue 21 22 23class DataOrder(Enum): 24 """A simple enumeration representing bit ordering.""" 25 MSB = True 26 LSB = False 27 28 29def main() -> None: 30 """Run the EnumValue demonstration. 31 32 Creates two :class:`EnumValue` instances: 33 1. Using an Enum member directly (`DataOrder.MSB`). 34 2. Using the underlying primitive value (`True`). 35 36 Both inputs should resolve to the same Enum member and have 37 `status = Status.OK`. 38 39 Prints: 40 * `"member → OK True"` 41 * `"underlying → OK True"` 42 """ 43 a = EnumValue(DataOrder.MSB, DataOrder) 44 b = EnumValue(True, DataOrder) 45 print("member →", a.status.name, a.value) 46 print("underlying →", b.status.name, b.value) 47 48 49if __name__ == "__main__": 50 main()
class
DataOrder(enum.Enum):
24class DataOrder(Enum): 25 """A simple enumeration representing bit ordering.""" 26 MSB = True 27 LSB = False
A simple enumeration representing bit ordering.
MSB =
<DataOrder.MSB: True>
LSB =
<DataOrder.LSB: False>
Inherited Members
- enum.Enum
- name
- value
def
main() -> None:
30def main() -> None: 31 """Run the EnumValue demonstration. 32 33 Creates two :class:`EnumValue` instances: 34 1. Using an Enum member directly (`DataOrder.MSB`). 35 2. Using the underlying primitive value (`True`). 36 37 Both inputs should resolve to the same Enum member and have 38 `status = Status.OK`. 39 40 Prints: 41 * `"member → OK True"` 42 * `"underlying → OK True"` 43 """ 44 a = EnumValue(DataOrder.MSB, DataOrder) 45 b = EnumValue(True, DataOrder) 46 print("member →", a.status.name, a.value) 47 print("underlying →", b.status.name, b.value)
Run the EnumValue demonstration.
Creates two EnumValue instances:
- Using an Enum member directly (
DataOrder.MSB). - Using the underlying primitive value (
True).
Both inputs should resolve to the same Enum member and have
status = Status.OK.
Prints:
"member → OK True""underlying → OK True"