Repository: Avaiga/taipy
File: taipy/gui/utils/_evaluator.py (Python)

Code before:
        except Exception as e:
            _warn(f"Exception raised evaluating {expr_string}", e)

    def re_evaluate_expr(self, gui: Gui, var_name: str) -> t.Set[str]: # noqa C901
        """
        This function will execute when the _update_var function is handling
        an expression with only a single variable
        """
        modified_vars: t.Set[str] = set()
        # Verify that the current hash is an edge case one (only a single variable inside the original expression)
        if var_name.startswith("tp_"):
            return modified_vars
        expr_original = None
        # if var_name starts with tpec_ --> it is an edge case with modified var
        if var_name.startswith("tpec_"):
            # backup for later reference
            var_name_original = var_name
            expr_original = self.__hash_to_expr[var_name]
            temp_expr_var_map = self.__expr_to_var_map[expr_original]
            if len(temp_expr_var_map) <= 1:
                index_in_array = int(m[0]) if (m := _Evaluator.__IS_ARRAY_EXPR_RE.findall(expr_original)) else -1
                # since this is an edge case --> only 1 item in the dict and that item is the original var
                var_name = next(iter(temp_expr_var_map.values()), var_name)
                # construct correct var_path to reassign values
                var_name_full, _ = _variable_decode(expr_original)
                var_name_full = var_name_full.split(".")
                var_name_full[0] = var_name
                var_name_full = ".".join(var_name_full)
                if index_in_array > - 1:
                    array_val =  _getscopeattr(gui, var_name)
                    if isinstance(array_val, list) and len(array_val) > index_in_array:
                        array_val[index_in_array] = _getscopeattr(gui, var_name_original)
                    else:
                        index_in_array = -1

                if index_in_array == -1:
                    _setscopeattr_drill(gui, var_name_full, _getscopeattr(gui, var_name_original))
            else:
                # multiple key-value pair in expr_var_map --> expr is special case a["b"]
                key = ""
                for v in temp_expr_var_map.values():
                    if isinstance(_getscopeattr(gui, v), _MapDict):
                        var_name = v
                    else:
                        key = v
                if key == "":
                    return modified_vars
                _setscopeattr_drill(gui, f"{var_name}.{_getscopeattr(gui, key)}", _getscopeattr(gui, var_name_original))
        # A middle check to see if var_name is from _MapDict
        if "." in var_name:
            var_name = var_name[: var_name.index(".")]

Review comment (suggestion):
maybe for better clarity: `next(iter(temp_expr_var_map.values()), None) if temp_expr_var_map else var_name`

Code after:
            _setscopeattr(gui, var_name, expr_evaluated)
            if holder is not None:
                holder.set(expr_evaluated)
        except Exception as e:
            _warn(f"Exception raised evaluating {expr_string}", e)

    def re_evaluate_expr(self, gui: Gui, var_name: str) -> t.Set[str]:  # noqa C901
        """
        This function will execute when the _update_var function is handling
        an expression with only a single variable
        """
        modified_vars: t.Set[str] = set()
        # Verify that the current hash is an edge case one (only a single variable inside the original expression)
        if var_name.startswith("tp_"):
            return modified_vars
        expr_original = None
        # if var_name starts with tpec_ --> it is an edge case with modified var
        if var_name.startswith("tpec_"):
            # backup for later reference
            var_name_original = var_name
            expr_original = self.__hash_to_expr[var_name]
            temp_expr_var_map = self.__expr_to_var_map[expr_original]
            if len(temp_expr_var_map) <= 1:
                index_in_array = int(m[0]) if (m := _Evaluator.__IS_ARRAY_EXPR_RE.findall(expr_original)) else -1
                # since this is an edge case --> only 1 item in the dict and that item is the original var
                var_name = next(iter(temp_expr_var_map.values()), var_name)
                # construct correct var_path to reassign values
                var_name_full, _ = _variable_decode(expr_original)
                var_name_full = var_name_full.split(".")
                var_name_full[0] = var_name
                var_name_full = ".".join(var_name_full)
                if index_in_array >= 0:
                    array_val = _getscopeattr(gui, var_name)
                    if isinstance(array_val, list) and len(array_val) > index_in_array:
                        array_val[index_in_array] = _getscopeattr(gui, var_name_original)
                    else:
                        index_in_array = -1

                if index_in_array < 0:
                    _setscopeattr_drill(gui, var_name_full, _getscopeattr(gui, var_name_original))
            else:
                # multiple key-value pair in expr_var_map --> expr is special case a["b"]
                key = ""
                for v in temp_expr_var_map.values():
                    if isinstance(_getscopeattr(gui, v), _MapDict):
                        var_name = v
                    else:
                        key = v
                if key == "":
                    return modified_vars
                _setscopeattr_drill(gui, f"{var_name}.{_getscopeattr(gui, key)}", _getscopeattr(gui, var_name_original))