Skip to content

Commit

Permalink
REFACT: Clean up variables.py file (#4554)
Browse files Browse the repository at this point in the history
  • Loading branch information
SMoraisAnsys authored Apr 24, 2024
2 parents 075d820 + 424ea97 commit c4b109d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
8 changes: 4 additions & 4 deletions _unittest/test_09_VariableManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ def test_07_addition(self):
v2 = Variable(3)
v3 = Variable("3mA")
v4 = Variable("10A")
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
v1 + v2

with pytest.raises(AssertionError):
with pytest.raises(ValueError):
v2 + v1
result_1 = v2 + v2
result_2 = v3 + v4
Expand All @@ -278,10 +278,10 @@ def test_08_subtraction(self):
v3 = Variable("3mA")
v4 = Variable("10A")

with pytest.raises(AssertionError):
with pytest.raises(ValueError):
v1 - v2

with pytest.raises(AssertionError):
with pytest.raises(ValueError):
v2 - v1

result_1 = v2 - v2
Expand Down
67 changes: 42 additions & 25 deletions pyaedt/application/Variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from __future__ import absolute_import # noreorder
from __future__ import division

import ast
import os
import re
import types
Expand Down Expand Up @@ -156,14 +157,17 @@ def __getitem__(self, item):
if variable in key_string:
found_variable = True
break
assert found_variable, "Input string {} is not a key of the data dictionary.".format(variable)
if not found_variable:
raise KeyError("Input string {} is not a key of the data dictionary.".format(variable))
data_out._data[variable] = self._data[key_string]
data_out._header.append(variable)
return data_out

@pyaedt_function_handler()
def __add__(self, other):
assert self.number_of_columns == other.number_of_columns, "Inconsistent number of columns"
if self.number_of_columns != other.number_of_columns:
raise ValueError("Number of columns is inconsistent.")

# Create a new object to return, avoiding changing the original inputs
new_dataset = CSVDataset()
# Add empty columns to new_dataset
Expand Down Expand Up @@ -198,7 +202,8 @@ def __iadd__(self, other):
for column in other.data:
self._data[column] = []

assert self.number_of_columns == other.number_of_columns, "Inconsistent number of columns"
if self.number_of_columns != other.number_of_columns:
raise ValueError("Number of columns is inconsistent.")

# Append the data from 'other'
for column, row_data in other.data.items():
Expand Down Expand Up @@ -1056,7 +1061,7 @@ def set_variable(
self._logger.clear_messages()
return
except Exception:
pass
self._logger.debug("Something went wrong when deleting '{}'.".format(variable_name))
else:
raise Exception("Unhandled input type to the design property or project variable.") # pragma: no cover

Expand Down Expand Up @@ -1188,7 +1193,7 @@ def delete_separator(self, separator_name):
)
return True
except Exception:
pass
self._logger.debug("Failed to change desktop object property.")
return False

@pyaedt_function_handler()
Expand Down Expand Up @@ -1229,7 +1234,7 @@ def delete_variable(self, var_name):
]
)
except Exception: # pragma: no cover
pass
self._logger.debug("Failed to change desktop object property.")
else:
self._cleanup_variables()
return True
Expand Down Expand Up @@ -1424,9 +1429,12 @@ def __init__(
self._value = self._calculated_value
# If units have been specified, check for a conflict and otherwise use the specified unit system
if units:
assert not self._units, "The unit specification {} is inconsistent with the identified units {}.".format(
specified_units, self._units
)
if self._units and self._units != specified_units:
raise RuntimeError(
"The unit specification {} is inconsistent with the identified units {}.".format(
specified_units, self._units
)
)
self._units = specified_units

if not si_value and is_number(self._value):
Expand Down Expand Up @@ -1493,7 +1501,7 @@ def _set_prop_val(self, prop, val, n_times=10):
break
i += 1
except Exception:
pass
self._app.logger.debug("Failed to set property '{}' value.".format(prop))

@pyaedt_function_handler()
def _get_prop_val(self, prop):
Expand All @@ -1515,7 +1523,7 @@ def _get_prop_val(self, prop):
name = "LocalVariables"
return self._app.get_oo_object(self._aedt_obj, "{}/{}".format(name, self._variable_name)).GetPropValue(prop)
except Exception:
pass
self._app.logger.debug("Failed to get property '{}' value.".format(prop))

@property
def name(self):
Expand Down Expand Up @@ -1724,7 +1732,7 @@ def expression(self, value):
def numeric_value(self):
"""Numeric part of the expression as a float value."""
if is_array(self._value):
return list(eval(self._value))
return list(ast.literal_eval(self._value))
try:
var_obj = self._aedt_obj.GetChildObject("Variables").GetChildObject(self._variable_name)
val, _ = decompose_variable_value(var_obj.GetPropEvaluatedValue("EvaluatedValue"))
Expand Down Expand Up @@ -1818,9 +1826,12 @@ def rescale_to(self, units):
"""
new_unit_system = unit_system(units)
assert (
new_unit_system == self.unit_system
), "New unit system {0} is inconsistent with the current unit system {1}."
if new_unit_system != self.unit_system:
raise ValueError(
"New unit system {} is inconsistent with the current unit system {}.".format(
new_unit_system, self.unit_system
)
)
self._units = units
return self

Expand Down Expand Up @@ -1891,7 +1902,9 @@ def __mul__(self, other):
>>> assert result_3.unit_system == "Power"
"""
assert is_number(other) or isinstance(other, Variable), "Multiplier must be a scalar quantity or a variable."
if not is_number(other) and not isinstance(other, Variable):
raise ValueError("Multiplier must be a scalar quantity or a variable.")

if is_number(other):
result_value = self.numeric_value * other
result_units = self.units
Expand Down Expand Up @@ -1936,10 +1949,11 @@ def __add__(self, other):
>>> assert result.unit_system == "Current"
"""
assert isinstance(other, Variable), "You can only add a variable with another variable."
assert (
self.unit_system == other.unit_system
), "Only ``Variable`` objects with the same unit system can be added."
if not isinstance(other, Variable):
raise ValueError("You can only add a variable with another variable.")
if self.unit_system != other.unit_system:
raise ValueError("Only Variable objects with the same unit system can be added.")

result_value = self.value + other.value
result_units = SI_UNITS[self.unit_system]
# If the units of the two operands are different, return SI-Units
Expand Down Expand Up @@ -1978,10 +1992,11 @@ def __sub__(self, other):
>>> assert result_2.unit_system == "Current"
"""
assert isinstance(other, Variable), "You can only subtract a variable from another variable."
assert (
self.unit_system == other.unit_system
), "Only ``Variable`` objects with the same unit system can be subtracted."
if not isinstance(other, Variable):
raise ValueError("You can only subtract a variable from another variable.")
if self.unit_system != other.unit_system:
raise ValueError("Only Variable objects with the same unit system can be subtracted.")

result_value = self.value - other.value
result_units = SI_UNITS[self.unit_system]
# If the units of the two operands are different, return SI-Units
Expand Down Expand Up @@ -2023,7 +2038,9 @@ def __truediv__(self, other):
>>> assert result_1.unit_system == "Current"
"""
assert is_number(other) or isinstance(other, Variable), "Divisor must be a scalar quantity or a variable."
if not is_number(other) and not isinstance(other, Variable):
raise ValueError("Divisor must be a scalar quantity or a variable.")

if is_number(other):
result_value = self.numeric_value / other
result_units = self.units
Expand Down

0 comments on commit c4b109d

Please sign in to comment.