Source code for atomiq.arguments.arguments
from copy import copy
import logging
from typing import Optional
logging.basicConfig()
logger = logging.getLogger(__name__)
[docs]
class ArgumentProvider:
static_arguments = {}
def __init__(self):
"""
Abstract base class to represent argument providers for experiments.
"""
pass
def _build_arguments(self, target_class: type, arguments: Optional[dict] = None) -> dict:
"""
Build the argument dict.
This is intended to be overwritten by subclasses to implement the logic for the respective backend
Args:
target_class: The class of the target for which to build the arguments
arguments: arguments to be considered additionally
"""
raise NotImplementedError("Implement getting the arguments dict in a subclass")
[docs]
def get_arguments(self, target_class: type) -> dict:
"""
Query the arguments dict from the argument provider.
Args:
target_class: The class of the target for which to build the arguments
Returns:
dict: Argument dictionary in the format required by atomiq
"""
logger.debug(f"Getting arguments for class {target_class.__name__}")
return self._build_arguments(target_class, copy(self.static_arguments))
[docs]
def update_argument(self, name: str, arg_def: dict):
"""
Update the argument definition in the backend of the argument provider, i.e. store updated values
Args:
name: Name or identifier of the argument
arg_def: dict with argument definition to be stored
"""
raise NotImplementedError("Implement updating in the backend in subclasses")
[docs]
class NativeArgumentProvider(ArgumentProvider):
def __init__(self):
"""
An argument provider that only returns the arguments defined by the `arguments` attribute in the target
class and joins them with `static_arguments` (if defined) of this argument provider.
"""
ArgumentProvider.__init__(self)
def _build_arguments(self, target_class: type, arguments: Optional[dict] = None) -> dict:
"""
Build the argument dict from the `arguments` attribute of the target class.
This enables to just write a dict with the argument definition in the experiment class
Args:
target_class: The class of the target for which to build the arguments
arguments: arguments to be considered additionally
"""
if arguments is None:
arguments = {}
if hasattr(target_class, "arguments"):
arguments.update(target_class.arguments)
return arguments