Better cast to string for Params

This commit is contained in:
Dominik Moritz Roth 2024-10-02 17:05:20 +02:00
parent 9e88f68fe0
commit ccd63de9ea

View File

@ -135,6 +135,9 @@ class Nucon(Enum):
self.param_type = param_type self.param_type = param_type
self.is_writable = is_writable self.is_writable = is_writable
def __str__(self):
return self.id
@property @property
def enum_type(self) -> Type[Enum]: def enum_type(self) -> Type[Enum]:
return self.param_type if issubclass(self.param_type, Enum) else None return self.param_type if issubclass(self.param_type, Enum) else None
@ -170,7 +173,10 @@ class Nucon(Enum):
return [param for param in cls if param.is_writable] return [param for param in cls if param.is_writable]
@classmethod @classmethod
def get(cls, parameter: 'Nucon') -> Union[float, int, bool, str, Enum]: def get(cls, parameter: Union['Nucon', str]) -> Union[float, int, bool, str, Enum]:
if isinstance(parameter, str):
parameter = cls[parameter]
if NuconConfig.dummy_mode: if NuconConfig.dummy_mode:
return cls._get_dummy_value(parameter) return cls._get_dummy_value(parameter)
@ -212,7 +218,10 @@ class Nucon(Enum):
return cls.get_multiple(list(cls)) return cls.get_multiple(list(cls))
@classmethod @classmethod
def set(cls, parameter: 'Nucon', value: Union[float, int, bool, str, Enum], force: bool = False) -> None: def set(cls, parameter: Union['Nucon', str], value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
if isinstance(parameter, str):
parameter = cls[parameter]
if not force and not parameter.is_writable: if not force and not parameter.is_writable:
raise ValueError(f"Parameter {parameter.name} is not writable") raise ValueError(f"Parameter {parameter.name} is not writable")
@ -226,7 +235,8 @@ class Nucon(Enum):
response = requests.post(NuconConfig.base_url, params={"variable": parameter.name, "value": str(value), "force": str(force).lower()}) response = requests.post(NuconConfig.base_url, params={"variable": parameter.name, "value": str(value), "force": str(force).lower()})
if response.status_code != 200: if response.status_code != 200:
raise E raise Exception(f"Failed to set parameter {parameter.name}. Status code: {response.status_code}")
# Example usage # Example usage
if __name__ == "__main__": if __name__ == "__main__":