Basic Enums Usage
This example demonstrates the basic usage of enums in the True-Core enums and toolkits module.
Example Code
Basic Enums Usage Example
1"""
2Examples of basic specialized enum types from the enums_toolkits module.
3"""
4
5from true.enums_toolkits import (
6 ByteEnum, FloatEnum, ComplexNumberEnum,
7 DictEnum, SetEnum, ListEnum, TupleEnum
8)
9
10
11def demo_numeric_enums():
12 """Demonstrate numeric-based enum types."""
13 print("=== Numeric Enum Types ===")
14
15 # Float enum example
16 class Temperature(FloatEnum):
17 FREEZING = 0.0
18 ROOM = 20.5
19 BOILING = 100.0
20
21 print("Temperature values:")
22 for temp in Temperature:
23 print(f"{temp.name}: {temp.value}°C")
24
25 # Complex number enum example
26 class ComplexPoints(ComplexNumberEnum):
27 ORIGIN = 0 + 0j
28 UNIT = 1 + 0j
29 DIAGONAL = 1 + 1j
30
31 print("\nComplex points:")
32 for point in ComplexPoints:
33 print(f"{point.name}: {point.value}")
34
35
36def demo_byte_enum():
37 """Demonstrate ByteEnum usage."""
38 print("\n=== Byte Enum ===")
39
40 class Flags(ByteEnum):
41 EMPTY = b'\x00'
42 START = b'\x01'
43 STOP = b'\x02'
44 RESET = b'\xFF'
45
46 print("Flag values:")
47 for flag in Flags:
48 print(f"{flag.name}: {flag.value.hex()}")
49
50
51def demo_collection_enums():
52 """Demonstrate collection-based enum types."""
53 print("\n=== Collection Enum Types ===")
54
55 # Dict enum example
56 class Config(DictEnum):
57 DEFAULT = {"debug": False, "timeout": 30}
58 DEVELOPMENT = {"debug": True, "timeout": 60}
59 PRODUCTION = {"debug": False, "timeout": 10}
60
61 print("Configuration options:")
62 for config in Config:
63 print(f"\n{config.name}:")
64 for key, value in config.value.items():
65 print(f" {key}: {value}")
66
67 # Set enum example
68 class Categories(SetEnum):
69 FRUITS = {"apple", "banana", "orange"}
70 VEGETABLES = {"carrot", "potato", "tomato"}
71 GRAINS = {"rice", "wheat", "oats"}
72
73 print("\nFood categories:")
74 for category in Categories:
75 print(f"\n{category.name}:")
76 for item in category.value:
77 print(f" - {item}")
78
79 # List enum example
80 class Sequences(ListEnum):
81 FIBONACCI = [1, 1, 2, 3, 5, 8, 13]
82 SQUARES = [1, 4, 9, 16, 25]
83 PRIMES = [2, 3, 5, 7, 11, 13]
84
85 print("\nNumber sequences:")
86 for sequence in Sequences:
87 print(f"{sequence.name}: {sequence.value}")
88
89 # Tuple enum example
90 class Points(TupleEnum):
91 ORIGIN = (0, 0)
92 UNIT_X = (1, 0)
93 UNIT_Y = (0, 1)
94
95 print("\nGeometric points:")
96 for point in Points:
97 print(f"{point.name}: {point.value}")
98
99
100def demo_type_validation():
101 """Demonstrate type validation of enum values."""
102 print("\n=== Type Validation ===")
103
104 try:
105 class InvalidTemp(FloatEnum):
106 INVALID = "20.5" # Should be float, not string
107 except Exception as e:
108 print(f"FloatEnum validation error: {e}")
109
110 try:
111 class InvalidDict(DictEnum):
112 INVALID = [1, 2, 3] # Should be dict, not list
113 except Exception as e:
114 print(f"DictEnum validation error: {e}")
115
116
117if __name__ == "__main__":
118 demo_numeric_enums()
119 demo_byte_enum()
120 demo_collection_enums()
121 demo_type_validation()
Key Features
Enum Creation - Basic enum definition - Enum value assignment - Enum inheritance
Enum Properties - Value access - Name access - Type checking
Basic Operations - Comparison operations - Iteration - Value lookup