|
|
""" |
|
|
Visualization state and configuration. |
|
|
|
|
|
Contains: |
|
|
- VisualizationState: Enum for tracking visualization state |
|
|
- VisualizationConfig: Configuration dataclass for visualization options |
|
|
""" |
|
|
|
|
|
from dataclasses import dataclass |
|
|
from enum import Enum |
|
|
|
|
|
|
|
|
class VisualizationState(Enum): |
|
|
""" |
|
|
π CONCEPT: Finite State Machine |
|
|
|
|
|
A visualization can only be in ONE of these states at a time. |
|
|
This prevents impossible states like "playing AND paused". |
|
|
|
|
|
BEFORE OOP (with strings): |
|
|
state = "idle" |
|
|
if state == "plying": # Typo! Hard to catch bug |
|
|
... |
|
|
|
|
|
AFTER OOP (with Enum): |
|
|
state = VisualizationState.IDLE |
|
|
if state == VisualizationState.PLYING: # Python error! Typo caught |
|
|
... |
|
|
""" |
|
|
IDLE = "idle" |
|
|
READY = "ready" |
|
|
PLAYING = "playing" |
|
|
PAUSED = "paused" |
|
|
STEPPING = "stepping" |
|
|
COMPLETE = "complete" |
|
|
|
|
|
|
|
|
@dataclass |
|
|
class VisualizationConfig: |
|
|
""" |
|
|
Configuration options for the visualizer. |
|
|
|
|
|
π CONCEPT: Configuration Object |
|
|
|
|
|
Instead of passing many parameters to functions, we bundle |
|
|
related settings into a configuration object. |
|
|
|
|
|
BEFORE: |
|
|
def start_visualization(speed, auto_play, loop, show_stats, ...): |
|
|
|
|
|
AFTER: |
|
|
def start_visualization(config: VisualizationConfig): |
|
|
|
|
|
Benefits: |
|
|
- Easy to add new options without changing function signatures |
|
|
- Can have sensible defaults |
|
|
- Can save/load configurations |
|
|
""" |
|
|
animation_speed_ms: int = 1000 |
|
|
auto_play: bool = False |
|
|
loop: bool = False |
|
|
show_statistics: bool = True |
|
|
show_legend: bool = True |
|
|
image_size: int = 60 |
|
|
|