"""Common instances of Option including most JSON types."""
from ..api.config import Option
[docs]class BoolOption(Option):
"""A boolean option."""
value_type = bool
[docs] def add_argument(self, parser):
"""Add an argument with an action to an argparse 'ArgumentParser'."""
if self.default:
option_prefix = "no-"
action = "store_false"
else:
option_prefix = ""
action = "store_true"
parser.add_argument(
f"--{option_prefix}{self.prefix}{self.name}",
action=action,
help=self.desc,
)
[docs]class IntOption(Option):
"""A single integer option."""
value_type = int
[docs]class StringOption(Option):
"""A string option."""
value_type = str
[docs]class FloatOption(Option):
"""A single float option."""
value_type = float
[docs]class EnumOption(Option):
"""An enum option represents a choice from a finite list."""
value_type = str
[docs] def __init__(self, desc, choices=None, **kwargs):
"""Construct option that records possible choices."""
self.choices = choices
Option.__init__(self, desc, **kwargs)
[docs] def set_value(self, value):
"""Restrict set values to choices."""
if value not in self.choices:
raise TypeError(
f"Value {str(type(value))} not one of the allowed "
f"choices for EnumOption. Choices are: {self.choices}"
)
Option.set_value(self, value)
[docs] def __str__(self):
"""Format string that shows choices."""
return (f"{self.__class__.__name__}(choices={self.choices}, "
f"name={self.name}, _value={self._value})")
[docs]class RandomSeedOption(IntOption):
"""
An :class:`IntOption` subclass specifically for random seeds.
This class makes it a bit easier to detect random seeds in large pipelines,
which should make studying variability due to controllable (RNG) randomness
straightforward.
"""
pass