BlockOption
The BlockOption class is a versatile data structure for defining various configuration options, such as checkboxes, inputs, sliders, and more. It enforces specific constraints based on the type of option being created, ensuring validity and consistency of the options’ data.
This dataclass is not used directly, but it’s initialized by the Block.add_option(name: str, type: str, ...parameters)
method. The following table shows what are the possible parameters to initialize the option.
Option Types
Below is a detailed breakdown of each option type supported:
Type | Additional Parameters | Notes |
---|---|---|
checkbox | value (default: False) | value must be a boolean. |
input | value (default: ‘Text input’) | value must be a string. |
integer | value , min (optional), max (optional) |
|
number | value , min (optional), max (optional) |
|
select | items , value (optional) |
|
slider | min , max , value (optional) |
|
display | value | value must be a string. |
Notes on Parameters
- name: A string representing the name of the option. This parameter is required for all option types.
- type: A string indicating the type of option (e.g., checkbox, input, integer). This parameter is required for all option types.
- value: The initial value of the option. The type and constraints of value depend on the type of the option.
- min: Define the range of acceptable values for integer, number, and slider options. They are optional for integer and number but required for slider.
- max: Define the range of acceptable values for integer, number, and slider options. They are optional for integer and number but required for slider.
- items: A list of strings used for select options. This parameter is required and must contain at least one item.
Validation
The build method performs rigorous validation for each option type:
- Ensures required parameters are provided.
- Checks that parameters conform to the expected types and constraints.
- Raises an AssertionError with an explanatory message if validation fails.
Example Usage
## ... existing code ...
my_block = Block()
# Creating a checkbox option
my_block.add_option(name="EnableFeature", type="checkbox", value=True)
# Creating a slider option
my_block.add_option(name="VolumeControl", type="slider", min=0, max=100, value=50)
# Creating a select option
my_block.add_option(name="ColorChoice", type="select", items=["Red", "Green", "Blue"], value="Green")