Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
eb67f23
Refactor _get_value_by_key method
YoungMasterGandalf Oct 21, 2024
93e7210
Refactor _join_dictionaries method
YoungMasterGandalf Oct 21, 2024
82eb23c
Refactor _delete_dict_entry method
YoungMasterGandalf Oct 21, 2024
948cbb1
Add return to _delete_dict_entry method
YoungMasterGandalf Oct 21, 2024
85de42a
Refactor _add_dict_entry method
YoungMasterGandalf Oct 21, 2024
2ff65c8
Refactor _invert_dictionary method
YoungMasterGandalf Oct 21, 2024
698df10
Refactor DictionaryModifyHandler direct_execute for DB var checking, …
YoungMasterGandalf Oct 21, 2024
2cbf535
Introduce _evaluate_argument method to AbstractFunctionHandler
YoungMasterGandalf Oct 21, 2024
ade4e06
Format DictionaryModifyHandler parts
YoungMasterGandalf Oct 21, 2024
04e3d09
Sort and clean variable_handlers.py imports
YoungMasterGandalf Oct 21, 2024
c247cad
Introduce validate_hashable_dict_key auciliary function
YoungMasterGandalf Oct 22, 2024
6c4bf77
Use validate_hashable_dict_key auxiliary func in DictionaryModifyHandler
YoungMasterGandalf Oct 22, 2024
abe6cd8
Reorder methods in DictModifyHandler to standard ordering
YoungMasterGandalf Oct 22, 2024
b06d51d
Delete execute_with_params from DictModifyHandler
YoungMasterGandalf Oct 22, 2024
e618ba7
Add pass_syntax_err argument to _evaluate_argument method
YoungMasterGandalf Oct 22, 2024
f26b098
Refactor NewVariableHandler
YoungMasterGandalf Oct 22, 2024
81fc955
Rename variable_name to var_name
YoungMasterGandalf Oct 22, 2024
9228a51
Rename variable_value to var_value
YoungMasterGandalf Oct 22, 2024
4c7482f
Fix passing of err in _evaluate_argument method
YoungMasterGandalf Oct 24, 2024
def48a6
Add new var name check to DictModifyHandler
YoungMasterGandalf Oct 30, 2024
d3e266b
Delete commented deprecated imports
YoungMasterGandalf Oct 31, 2024
3589b6f
Delete commented code
YoungMasterGandalf Oct 31, 2024
46ac1cc
Remove unused input_execute
YoungMasterGandalf Oct 31, 2024
8ad401f
Improve MathModify direct_execute
YoungMasterGandalf Oct 31, 2024
1a7c0e7
Format MathModify init_docs
YoungMasterGandalf Oct 31, 2024
5142e1f
Improve resolve_type_error_list error handling
YoungMasterGandalf Oct 31, 2024
a9119d6
Rename variable_type to new_var_type
YoungMasterGandalf Oct 31, 2024
66b816b
Delete ListModifyVariable's _evaluate_argument method
YoungMasterGandalf Nov 9, 2024
c0c4ac3
Change empty string evaluation to return None
YoungMasterGandalf Nov 9, 2024
0373d52
Refactor ListModifyVariableHandler execution logic
YoungMasterGandalf Nov 9, 2024
36a72a2
Format code
YoungMasterGandalf Nov 9, 2024
b55589b
Merge branch 'main' into daniel/fix-core-var-handlers
YoungMasterGandalf Nov 9, 2024
f37798b
Fix syntax mistakes
YoungMasterGandalf Nov 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import inspect
import ast
import re
from typing import Union
from typing import Union, Any

import forloop_modules.globals.variable_handler as vh # DISABLED IN FORLOOP MODULES
import forloop_modules.queries.node_context_requests_backend as ncrb # DISABLED IN FORLOOP MODULES


from forloop_modules.errors.errors import CriticalPipelineError
from forloop_modules.function_handlers.auxilliary.node_type_categories_manager import ntcm
from forloop_modules.globals.variable_handler import defined_functions_dict # DISABLED IN FORLOOP MODULES
from forloop_modules.node_detail_form import NodeField, NodeParams
Expand All @@ -32,6 +32,7 @@ class AbstractFunctionHandler(abc.ABC):
type_category=ntcm.categories.unknown

def __init__(self):
self.icon_type = None
self.is_cloud_compatible: bool = True
self.is_disabled: bool = False
self.code_import_patterns = []
Expand Down Expand Up @@ -140,7 +141,54 @@ def replace_strings(self, text, replacements):
text = re.sub(pattern, replacement, text)

return text


def _evaluate_argument(self, arg: Any, pass_syntax_err: bool = False) -> Union[Any, None]:
"""
Safely evaluates the provided argument and returns the appropriate value.

Attempts to evaluate the argument using `ast.literal_eval`. If successful,
returns the evaluated value. If a `ValueError` or `TypeError` occurs,
returns the argument unchanged. Raises `CriticalPipelineError` for
`SyntaxError`, `MemoryError`, or `RecursionError`.

If `arg` is an empty string, i.e. `arg == ""` it is evaluated as None.

If `pass_syntax_err` == True, `CriticalPipelineError` is NOT raised in case of `SyntaxError`.

Args:
arg (Any): The input to evaluate.
pass_syntax_err (bool, optional): Flag determining `SyntaxError` behaviour. If True, arg
is returned as is in case of a `SyntaxError` (useful for arguments containg queries,
code snippets etc.), else a `CriticalPipelineError` gets raised. Defaults to False.

Returns:
Any: Evaluated value or the original argument.

Raises:
CriticalPipelineError: For critical errors during evaluation.
"""

# Return None for empty strings
is_arg_non_empty_str = arg and isinstance(arg, str)
if not is_arg_non_empty_str:
return None

try:
return ast.literal_eval(arg)
except (ValueError, TypeError):
return arg
except SyntaxError as e:
if pass_syntax_err:
return arg
else:
raise CriticalPipelineError(
f"{self.icon_type}: invalid parameter value passed as input: '{arg}'."
) from e
except (MemoryError, RecursionError) as e:
raise CriticalPipelineError(
f"{self.icon_type}: invalid parameter value passed as input: '{arg}'."
) from e

def _replace_key_with_variable_name_in_code(self, code: str, key: str, variable_name: str):
code = code.replace(key, variable_name)
# code = self.replace_strings(code, {key: variable_name})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import pandas as pd
from collections.abc import Hashable
from typing import Any

import numpy as np
import pandas as pd

import forloop_modules.flog as flog
from forloop_modules.errors.errors import CriticalPipelineError


def validate_hashable_dict_key(key: Any):
"""
Ensures the provided key is hashable. Raises a CriticalPipelineError if the key is unhashable.

Args:
key (Any): The key to check.

Raises:
CriticalPipelineError: If the key is not hashable.
"""
if not isinstance(key, Hashable):
raise CriticalPipelineError(f"provided dictionary key is unhashable: {key}")

def validate_input_data_types(df):
"""
Expand Down
Loading