[docs]classExperimentContainerWrapper(Generic[_E]):"""A wrapper for :class:`~qurry.qurrium.container.experiments.ExperimentContainer` for :class:`~qurry.qurrium.qurrium.QurriumPrototype.`. and its corresponding :class:`~qurry.qurrium.multimanager.multimanager.MultiManager`."""__name__="ExperimentContainerWrapper"__slots__=("_orphan_exps","_multimanagers","_all_exps_container")_orphan_exps:ExperimentContainer[_E]_multimanagers:dict[str,MultiManager[_E]]def__init__(self,orphan_exps:ExperimentContainer[_E],multimanagers:dict[str,MultiManager[_E]],):"""Initialize the wrapper with orphan experiments and their corresponding multimanagers. Args: orphan_exps: The orphan experiments container to be wrapped. multimanagers: The dictionary of multimanagers in QurriumPrototype. """self._orphan_exps=orphan_expsself._multimanagers=multimanagersself._all_exps_container={}@propertydefall_exps_container(self)->dict[str,ExperimentContainer[_E]]:"""Get all experiment containers. Returns: dict[str, ExperimentContainer[_ExpInst]]: The dictionary of all experiment containers. """iflist(self._all_exps_container.keys())==(["orphan_exps"]+list(self._multimanagers.keys())):returnself._all_exps_containerall_exps_container={"orphan_exps":self._orphan_exps}forcurrent_multimanager_id,current_multimanagerinself._multimanagers.items():all_exps_container[current_multimanager_id]=current_multimanager.expsself._all_exps_container=all_exps_containerreturnall_exps_containerdef__getitem__(self,key:str)->_E:"""Get the experiments from the container by key. Args: key (str): The key of the experiment to be retrieved. Returns: ExperimentContainer[_ExpInst]: The experiment container with the given key. """forcontainerinself.all_exps_container.values():ifkeyincontainer:returncontainer[key]raiseKeyError(f"Experiment id: '{key}' not found in any container.")
[docs]defwhere(self,key:str)->str:"""Get the experiment container where the experiment is located. Args: key (str): The key of the experiment to be retrieved. Returns: str: The container where the experiment is located. """forcontainer_id,containerinself.all_exps_container.items():ifkeyincontainer:returncontainer_idraiseKeyError(f"Experiment id: '{key}' not found in any container.")
def__setitem__(self,key:str,value:Any):"""Set the experiment in the container by key. Args: key (str): The key of the experiment to be set. value (Any): The value to be set. Raises: ValueError: If the key is found in any container. ValueError: If the key is found in orphan_exps. KeyError: If the key is not found in any container. """which_container=Noneforcontainer_id,containerinself.all_exps_container.items():ifkeyincontainer:which_container=container_idbreakifwhich_container=="orphan_exps":raiseValueError(f"You cannot set the experiment '{key}' in orphan_exps. "+"Please set it in the orphan_exps container.")ifwhich_containerisnotNone:raiseValueError(f"You cannot set the experiment '{key}' in experiment container wrapper. "+"But you can set it in the experiment container "+f"from multimanagers id: {which_container}.")raiseKeyError(f"Experiment id: '{key}' not found in any container.")
[docs]defitems(self):"""Get all experiments from all experiment containers. Returns: dict[str, _ExpInst]: A dictionary of all experiments in the container. """all_exps={}forcontainerinself.all_exps_container.values():all_exps.update(container.items())returnall_exps
def__repr__(self):"""Return the string representation of the wrapper. Returns: str: The string representation of the wrapper. """num_exps=sum(len(container)forcontainerinself.all_exps_container.values())returnf"{self.__name__}(num_exps={num_exps}, num_container={len(self.all_exps_container)})"def_repr_oneline(self):num_exps=sum(len(container)forcontainerinself.all_exps_container.values())returnf"{self.__name__}(num_exps={num_exps}, num_container={len(self.all_exps_container)})"def_repr_pretty_(self,p,cycle):length=len(self.all_exps_container)ifcycle:p.text(f"{self.__name__}("+"{...}"+f", num={length})")else:withp.group(2,f"{self.__name__}(num={length}"+", {","})"):fori,(k,v)inenumerate(self.all_exps_container.items()):p.breakable()# pylint: disable=protected-accessp.text(f"'{k}': {v._repr_oneline()}")# pylint: enable=protected-accessifi<length-1:p.text(",")