39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
# SPDX-FileCopyrightText: 2025 2025
|
|
# SPDX-FileContributor: Gergely Polonkai
|
|
#
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Random Choice Oracle"""
|
|
|
|
import random
|
|
from typing import Any
|
|
|
|
from .base import Oracle
|
|
|
|
|
|
class RandomChoiceOracle(Oracle):
|
|
"""An Oracle that can choose randomly from a list of possibilities"""
|
|
|
|
TYPE_MARKER = "random-choice"
|
|
|
|
def __init__(self, oracle_data: dict[str, Any]) -> None:
|
|
self.choices: list[str] | None = None
|
|
|
|
super().__init__(oracle_data)
|
|
|
|
def parse_and_validate(self, oracle_data: dict[str, Any]) -> None:
|
|
super().parse_and_validate(oracle_data)
|
|
|
|
if "choices" not in oracle_data:
|
|
raise KeyError("choices is missing from the data")
|
|
|
|
if not oracle_data["choices"]:
|
|
raise ValueError("Choice list cannot be empty")
|
|
|
|
if not isinstance(oracle_data["choices"], list):
|
|
raise TypeError("choices must be a list")
|
|
|
|
self.choices = oracle_data["choices"]
|
|
|
|
def generate(self) -> str:
|
|
return random.choice(self.choices or [])
|