Skip to content

Commit

Permalink
Made simpler the _variable_dict method. (#2923)
Browse files Browse the repository at this point in the history
* Made simpler the _variable_dict method.

* Made simpler the _variable_dict method.

* Made simpler the _variable_dict method.

* Update pyaedt/application/Variables.py

Co-authored-by: Maxime Rey <[email protected]>

* Update pyaedt/application/Variables.py

Co-authored-by: Maxime Rey <[email protected]>

---------

Co-authored-by: maxcapodi78 <Shark78>
Co-authored-by: Maxime Rey <[email protected]>
  • Loading branch information
maxcapodi78 and MaxJPRey authored Apr 27, 2023
1 parent 9ce2853 commit c959f7a
Showing 1 changed file with 69 additions and 73 deletions.
142 changes: 69 additions & 73 deletions pyaedt/application/Variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,21 +731,38 @@ def _logger(self):
def __init__(self, app):
# Global Desktop Environment
self._app = app
self._independent_variables = {}
self._independent_design_variables = {}
self._independent_project_variables = {}
self._dependent_variables = {}
self._dependent_design_variables = {}
self._dependent_project_variables = {}

@property
def _independent_variables(self):
all = {}
all.update(self._independent_project_variables)
all.update(self._independent_design_variables)
return all

@property
def _dependent_variables(self):
all = {}
for k, v in self._dependent_project_variables.items():
all[k] = v
for k, v in self._dependent_design_variables.items():
all[k] = v
return all

@property
def _all_variables(self):
all = {}
all.update(self._independent_variables)
all.update(self._dependent_variables)
return all

@pyaedt_function_handler()
def __delitem__(self, key):
"""Implement del with array name or index."""
self.delete_variable(key)
if key in self._independent_variables:
del self._independent_variables[key]
elif key in self._dependent_variables:
del self._dependent_variables[key]

@pyaedt_function_handler()
def __getitem__(self, variable_name):
Expand All @@ -756,6 +773,20 @@ def __setitem__(self, variable, value):
self.set_variable(variable, value)
return True

@pyaedt_function_handler()
def _cleanup_variables(self):
variables = self._get_var_list_from_aedt(self._app.odesign) + self._get_var_list_from_aedt(self._app.oproject)
all_dicts = [
self._independent_project_variables,
self._independent_design_variables,
self._dependent_project_variables,
self._dependent_design_variables,
]
for dict_var in all_dicts:
for var_name in list(dict_var.keys()):
if var_name not in variables:
del dict_var[var_name]

@pyaedt_function_handler()
def _variable_dict(self, object_list, dependent=True, independent=True):
"""Retrieve the variable dictionary.
Expand All @@ -777,67 +808,39 @@ def _variable_dict(self, object_list, dependent=True, independent=True):
"""
all_names = {}
for obj in object_list:
variables = self._get_var_list_from_aedt(obj)
variables = [i for i in self._get_var_list_from_aedt(obj) if i not in list(self._all_variables.keys())]
for variable_name in variables:
if independent and variable_name not in self._independent_variables:
if self.get_expression(variable_name):
variable_expression = self.get_expression(variable_name)
all_names[variable_name] = variable_expression
si_value = self._app.get_evaluated_value(variable_name)
value = Variable(
variable_expression, None, si_value, all_names, name=variable_name, app=self._app
)
is_number_flag = is_number(value._calculated_value)
if is_number_flag:
self._independent_variables[variable_name] = value
if obj == self._app.odesign:
self._independent_design_variables[variable_name] = value
elif "$" in variable_name:
self._independent_project_variables[variable_name] = value
elif dependent and not is_number_flag:
self._dependent_variables[variable_name] = value
if obj == self._app.odesign:
self._dependent_design_variables[variable_name] = value
elif "$" in variable_name:
self._dependent_project_variables[variable_name] = value
elif dependent and variable_name not in self._dependent_variables:
if self.get_expression(variable_name):
variable_expression = self.get_expression(variable_name)
all_names[variable_name] = variable_expression
si_value = self._app.get_evaluated_value(variable_name)
value = Variable(
variable_expression, None, si_value, all_names, name=variable_name, app=self._app
)
is_number_flag = is_number(value._calculated_value)
if not is_number_flag:
self._dependent_variables[variable_name] = value
if obj == self._app.odesign:
self._dependent_design_variables[variable_name] = value
elif "$" in variable_name:
self._dependent_project_variables[variable_name] = value
variable_expression = self.get_expression(variable_name)
if variable_expression:
all_names[variable_name] = variable_expression
si_value = self._app.get_evaluated_value(variable_name)
value = Variable(variable_expression, None, si_value, all_names, name=variable_name, app=self._app)
is_number_flag = is_number(value._calculated_value)
if variable_name.startswith("$") and is_number_flag:
self._independent_project_variables[variable_name] = value
elif variable_name.startswith("$"):
self._dependent_project_variables[variable_name] = value
elif is_number_flag:
self._independent_design_variables[variable_name] = value
else:
self._dependent_design_variables[variable_name] = value
self._cleanup_variables()
vars_to_output = {}
dicts_to_add = []
if independent:
if len(object_list) != 1:
vars = self._independent_variables.copy()
else:
if obj == self._app.odesign:
vars = self._independent_design_variables.copy()
else:
vars = self._independent_project_variables.copy()
else:
vars = {}
if self._app.odesign in object_list:
dicts_to_add.append(self._independent_design_variables)
if self._app.oproject in object_list:
dicts_to_add.append(self._independent_project_variables)
if dependent:
if len(object_list) != 1:
for k, v in self._dependent_variables.items():
vars[k] = v
else:
if obj == self._app.odesign:
for k, v in self._dependent_design_variables.items():
vars[k] = v
else:
for k, v in self._dependent_project_variables.items():
vars[k] = v

return vars
if self._app.odesign in object_list:
dicts_to_add.append(self._dependent_design_variables)
if self._app.oproject in object_list:
dicts_to_add.append(self._dependent_project_variables)
for dict_var in dicts_to_add:
for k, v in dict_var.items():
vars_to_output[k] = v
return vars_to_output

@pyaedt_function_handler()
def get_expression(self, variable_name): # TODO: Should be renamed to "evaluate"
Expand Down Expand Up @@ -1094,7 +1097,7 @@ def set_variable(
],
]
)
self._clean_variable_from_dicts(variable_name)
self._cleanup_variables()
var_list = self._get_var_list_from_aedt(desktop_object)
lower_case_vars = [var_name.lower() for var_name in var_list]
if variable_name.lower() not in lower_case_vars:
Expand Down Expand Up @@ -1182,17 +1185,10 @@ def delete_variable(self, var_name):
except: # pragma: no cover
pass
else:
self._clean_variable_from_dicts(var_name)
self._cleanup_variables()
return True
return False

@pyaedt_function_handler()
def _clean_variable_from_dicts(self, variable_name):
if variable_name in self._independent_variables:
del self._independent_variables[variable_name]
elif variable_name in self._dependent_variables:
del self._dependent_variables[variable_name]

@pyaedt_function_handler()
def _get_var_list_from_aedt(self, desktop_object):
var_list = []
Expand Down

0 comments on commit c959f7a

Please sign in to comment.