Enum Registry Operations
This example demonstrates various operations available in the enum registry module.
Example Code
Enum Registry Operations Example
1"""
2Examples of performing operations on EnumRegistry instances.
3"""
4
5from enum import Enum
6
7from true.enum_registry import EnumRegistry
8
9
10# Define example enums
11class Colors(Enum):
12 RED = "red"
13 GREEN = "green"
14 BLUE = "blue"
15
16
17class ExtendedColors(Enum):
18 RED = "red" # Duplicate value
19 YELLOW = "yellow"
20 PURPLE = "purple"
21
22
23class Numbers(Enum):
24 ONE = 1
25 TWO = 2
26 THREE = 3
27
28
29def demo_arithmetic():
30 """Demonstrate arithmetic operations between registries."""
31 print("=== Arithmetic Operations ===")
32
33 # Create base registries
34 colors = EnumRegistry([Colors, ExtendedColors], duplication=True)
35 print(colors)
36 numbers = EnumRegistry([Numbers])
37 #
38 # Addition
39 print("Addition:")
40 combined = colors + numbers
41 print(combined)
42 print("Colors + Extended Colors members:")
43 for member, metadata in combined:
44 print(f"- {member} = {metadata}")
45 #
46 # Subtraction
47 print("\nSubtraction:")
48 difference = combined - numbers
49 print("Combined - Colors members:")
50 for member, metadata in difference:
51 print(f"- {member} = {metadata}")
52
53
54#
55
56#
57def demo_value_analysis():
58 """Demonstrate value analysis features."""
59 print("\n=== Value Analysis ===")
60 #
61 registry = EnumRegistry([Colors, ExtendedColors, Numbers], duplication=True)
62 #
63 # # Find duplicates
64 print("Duplicate values:")
65 duplicates = registry.values.duplicates()
66 for value, members in duplicates.items():
67 print(f"Value '{value}' is used by:")
68 for member in members:
69 print(f"- {member.name} from {member.__class__.__name__}")
70 #
71 # Value statistics
72 print("\nValue statistics:")
73 stats = registry.statistics()
74 print(f"Total members: {stats.total_members}")
75 print(f"Unique values: {stats.unique_values}")
76 print(f"Name conflicts: {stats.name_conflicts}")
77 #
78 # # Value grouping
79 print("\nMembers grouped by value type:")
80 by_type = registry.types.group()
81 for type_name, members in by_type.items():
82 print(f"\n{type_name}:")
83 for member in members:
84 print(f"- {member.name} = {member.value}")
85
86
87if __name__ == "__main__":
88 demo_arithmetic()
89 demo_value_analysis()
Key Features
Registry Management - Batch registration of enums - Registry cleanup operations - Registry state management
Enum Operations - Enum value comparison - Enum type checking - Enum value validation
Error Handling - Duplicate registration handling - Invalid enum type handling - Registry state validation