Skip to content

Commit

Permalink
Long's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Lefévère-Laoide authored and Fred Lefévère-Laoide committed Sep 26, 2024
1 parent a7bdbbe commit 1e1b2c5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion taipy/gui/_renderers/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
_get_client_var_name,
_get_data_type,
_get_expr_var_name,
_get_lambda_id,
_getscopeattr,
_getscopeattr_drill,
_is_boolean,
Expand Down Expand Up @@ -167,7 +168,7 @@ def _get_variable_hash_names(
if val.__name__ == "<lambda>":
# if it is a lambda and it has already a hash_name, we're fine
if looks_like_a_lambda or not hash_name:
hash_name = gui._get_lambda_id(val)
hash_name = _get_lambda_id(val)
gui._bind_var_val(hash_name, val) # type: ignore[arg-type]
else:
hash_name = _get_expr_var_name(val.__name__)
Expand Down
4 changes: 2 additions & 2 deletions taipy/gui/builder/_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ def __parse_lambda_property(self, key: str, value: t.Any) -> t.Any:
return None
args = [arg.arg for arg in lambda_fn.args.args]
targets = [
compr.target.id # type: ignore[attr-defined]
comprehension.target.id # type: ignore[attr-defined]
for node in ast.walk(lambda_fn.body)
if isinstance(node, ast.ListComp)
for compr in node.generators
for comprehension in node.generators
]
tree = _TransformVarToValue(self.__calling_frame, args + targets + _python_builtins).visit(lambda_fn)
ast.fix_missing_locations(tree)
Expand Down
3 changes: 0 additions & 3 deletions taipy/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,9 +1652,6 @@ def _is_in_brdcst_callback(self):
except RuntimeError:
return False

def _get_lambda_id(self, lambda_fn: LambdaType):
return f"__lambda_{id(lambda_fn)}"

# Proxy methods for Evaluator
def _evaluate_expr(
self, expr: str, lazy_declare: t.Optional[bool] = False, lambda_expr: t.Optional[bool] = False
Expand Down
1 change: 1 addition & 0 deletions taipy/gui/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
_setscopeattr,
_setscopeattr_drill,
)
from ._lambda import _get_lambda_id
from ._locals_context import _LocalsContext
from ._map_dict import _MapDict
from ._runtime_manager import _RuntimeManager
Expand Down
3 changes: 2 additions & 1 deletion taipy/gui/utils/_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from . import (
_get_client_var_name,
_get_expr_var_name,
_get_lambda_id,
_getscopeattr,
_getscopeattr_drill,
_hasscopeattr,
Expand Down Expand Up @@ -262,7 +263,7 @@ def evaluate_expr(
_warn(f"Cannot evaluate expression '{not_encoded_expr if is_edge_case else expr_string}'", e)
expr_evaluated = None
if lambda_expr and callable(expr_evaluated):
expr_hash = gui._get_lambda_id(expr_evaluated)
expr_hash = _get_lambda_id(expr_evaluated)
# save the expression if it needs to be re-evaluated
return self.__save_expression(gui, expr, expr_hash, expr_evaluated, var_map, lambda_expr)

Expand Down
16 changes: 16 additions & 0 deletions taipy/gui/utils/_lambda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2021-2024 Avaiga Private Limited
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from types import LambdaType


def _get_lambda_id(lambda_fn: LambdaType):
return f"__lambda_{id(lambda_fn)}"

0 comments on commit 1e1b2c5

Please sign in to comment.