Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-28 07:12:52

0001 from typing import Dict, List, Optional, Tuple, Union, Literal, Any
0002 from pydantic import BaseModel, Field
0003 
0004 # Base class for all design parameters
0005 class BaseParameter(BaseModel):
0006     """
0007     Abstract base class for all parameter types.
0008     Subclasses should define specific parameter characteristics.
0009     """
0010     name: str
0011     type: str  # Discriminator
0012     value: Union[float, str, int]  # Generic value field
0013     
0014     class Config:
0015         extra = "allow"  # Allow subclasses to add fields
0016 
0017 
0018 class RangeParameter(BaseParameter):
0019     """Continuous parameter with min/max bounds."""
0020     value: float
0021     bounds: Tuple[float, float]
0022 
0023     @property
0024     def type(self) -> Literal["range"]:
0025         return "range"
0026 
0027 
0028 class ChoiceParameter(BaseParameter):
0029     """Categorical parameter with discrete choices."""
0030     value: str
0031     choices: List[str]
0032 
0033     @property
0034     def type(self) -> Literal["choice"]:
0035         return "choice"
0036 
0037 
0038 # Generic parameter union (for simple use cases)
0039 Parameter = Union[RangeParameter, ChoiceParameter]
0040 
0041 
0042 def parse_parameter(name: str, data: dict) -> Parameter:
0043     """
0044     Parses a raw dictionary into a Parameter object (RangeParameter or ChoiceParameter).
0045     Automatically injects the name into the data.
0046     """
0047     data["name"] = name
0048 
0049     if "bounds" in data:
0050         try:
0051             return RangeParameter(**data)
0052         except Exception as e:
0053             raise ValueError(f"Invalid range parameter '{name}': {e}")
0054 
0055     if "choices" in data:
0056         if not isinstance(data.get("value"), str):
0057             raise ValueError(
0058                 f"Parameter '{name}' appears to be a choice parameter, but its value `{data.get('value')}` is not a string. "
0059                 f'Wrap it in quotes: value: "{data.get("value")}"'
0060             )
0061         try:
0062             return ChoiceParameter(**data)
0063         except Exception as e:
0064             raise ValueError(f"Invalid choice parameter '{name}': {e}")
0065 
0066     raise ValueError(
0067         f"Parameter '{name}' must have either 'bounds' (for range) or 'choices' (for choice)."
0068     )
0069 
0070 class ContainerConfig(BaseModel):
0071     bind_paths: List[str] = Field(default_factory=list)
0072     environment_vars: Dict[str, str] = Field(default_factory=dict)