Skip to content

Commit

Permalink
MAINT: Let _get_enum_value raise exception on error by default
Browse files Browse the repository at this point in the history
  • Loading branch information
rfahlberg committed Dec 16, 2024
1 parent 821ab75 commit 7792aa6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
37 changes: 32 additions & 5 deletions src/ansys/optislang/core/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,51 @@
from typing import Any


def _get_enum_value(enum_value: Any, default_value: str = "") -> str:
def _get_enum_value(enum_value: Any, default_value: str = "", throw_on_error: bool = True) -> str:
"""Get value of an enum encoded in oSL Json.
Parameters
----------
enum_value : Any
The enum encoded in oSL Json
The enum encoded in oSL Json. Can be either a plain string
or a dict in the format ``{ "enum" : [], "value" : "" }``.
default_value : str
The default value if not present or convertible.
Returns
-------
str
The decoded enum value. If not present or convertible, ``default_value`` is returned
The decoded enum value. If not present or convertible and throw_on_error is False,
``default_value`` is returned, otherwise, an exception it raised.
Raises
------
TypeError
Raised when the decoded enum_value is not a string.
ValueError
Raised when the value for the ``string`` is invalid.
"""
if isinstance(enum_value, dict):
return enum_value.get("value", default_value)
value = enum_value.get("value")
if value is None:
if throw_on_error:
raise ValueError(
f"Cannot decode {enum_value} as enum: Doesn't contain the 'value' entry."
)
else:
return default_value
elif isinstance(value, str):
return value
else:
if throw_on_error:
raise TypeError(
f"Cannot decode {enum_value} as enum: 'value' entry is not of type str."
)
else:
return default_value
elif isinstance(enum_value, str):
return enum_value
else:
return default_value
if throw_on_error:
raise TypeError(f"Cannot decode {enum_value} as enum: str or dict expected.")
else:
return default_value
21 changes: 6 additions & 15 deletions src/ansys/optislang/core/project_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def _extract_criterion_properties_from_dict(criterion_dict: dict) -> Dict[str, A
expression = criterion_dict["lhs"]
expression_value_type = (
CriterionValueType.from_str(
_get_enum_value(criterion_dict["lhs_value"].get("kind", {}), "UNINITIALIZED")
_get_enum_value(criterion_dict["lhs_value"].get("kind", {}), "UNINITIALIZED", False)
)
if criterion_dict.get("lhs_value")
else None
Expand All @@ -744,7 +744,7 @@ def _extract_criterion_properties_from_dict(criterion_dict: dict) -> Dict[str, A
limit_expression = criterion_dict.get("rhs") # optional
limit_expression_value_type = (
CriterionValueType.from_str(
_get_enum_value(criterion_dict["rhs_value"].get("kind", {}), "UNINITIALIZED")
_get_enum_value(criterion_dict["rhs_value"].get("kind", {}), "UNINITIALIZED", False)
)
if criterion_dict.get("rhs_value")
else None
Expand All @@ -769,7 +769,7 @@ def _extract_criterion_properties_from_dict(criterion_dict: dict) -> Dict[str, A

value_type = value_type = (
CriterionValueType.from_str(
_get_enum_value(criterion_dict["value"].get("kind", {}), "UNINITIALIZED")
_get_enum_value(criterion_dict["value"].get("kind", {}), "UNINITIALIZED", False)
)
if criterion_dict.get("value")
else None
Expand Down Expand Up @@ -1944,30 +1944,21 @@ def _extract_parameter_properties_from_dict(par_dict: dict) -> dict:
"operation": par_dict.get("dependency_expression", ""),
"deterministic_resolution": (
ParameterResolution.from_str(
_get_enum_value(
par_dict["deterministic_property"].get("kind", {}),
"CONTINUOUS",
)
_get_enum_value(par_dict["deterministic_property"].get("kind", {}))
)
if par_dict.get("deterministic_property")
else ParameterResolution.CONTINUOUS
),
"stochastic_resolution": (
ParameterResolution.from_str(
_get_enum_value(
par_dict["stochastic_property"].get("kind", {}),
"MARGINALDISTRIBUTION",
)
_get_enum_value(par_dict["stochastic_property"].get("kind", {}))
)
if par_dict.get("stochastic_property")
else ParameterResolution.MARGINALDISTRIBUTION
),
"distribution_type": (
DistributionType.from_str(
_get_enum_value(
par_dict["stochastic_property"].get("type", {}),
"NORMAL",
)
_get_enum_value(par_dict["stochastic_property"].get("type", {}))
)
if par_dict.get("stochastic_property")
else DistributionType.NORMAL
Expand Down

0 comments on commit 7792aa6

Please sign in to comment.