(
        expression: Union[str, ast.Expression] = "",
        available_feature_names: List[str] = None,
        functions: List[str] = None,
        sanitizer: Callable = None,
        env: Optional[Dict[str, Any]] = None
)
return eval(compile(exp, "<lambda>", mode="eval"), env), params, vars_
Create a lambda function from a string expression.

    Parameters
    ----------
    expression : str or ast.Expression
        Right side of a modeling function.
    available_feature_names : list of str
        List of all available features names.
        Needed to distinguish between parameters, features and functions.
    functions : list of str
        List of all available functions.
        Needed to distinguish between parameters, features and functions.
    sanitizer : callable, optional
        Function for sanitizing variable names.
    env : dict, optional
        An environment to capture in the lambda's closure.

    Returns
    -------
    func : callable
        The created lambda function.
    params : list of str
        The recognied parameters withint the expression.
    vars_ : list of str
        The recognied variables withint the expression.

    Examples
    --------
    >>> from Orange.data import Table
    >>> data = Table("housing")
    >>> sfun = "a * exp(-b * CRIM * LSTAT) + c"
    >>> names = [a.name for a in data.domain.attributes]
    >>> func, par, var = _create_lambda(sfun, available_feature_names=names,
    ...                                 functions=["exp"], env={"exp": np.exp})
    >>> y = func(data.X, 1, 2, 3)
    >>> par
    ['a', 'b', 'c']
    >>> var
    ['CRIM', 'LSTAT']
Create a lambda function from a string expression.
Create a lambda function from a string expression .
def _create_lambda(
        expression: Union[str, ast.Expression] = "",
        available_feature_names: List[str] = None,
        functions: List[str] = None,
        sanitizer: Callable = None,
        env: Optional[Dict[str, Any]] = None
) -> Tuple[Callable, List[str], List[str]]:
    """
    Create a lambda function from a string expression.

    Parameters
    ----------
    expression : str or ast.Expression
        Right side of a modeling function.
    available_feature_names : list of str
        List of all available features names.
        Needed to distinguish between parameters, features and functions.
    functions : list of str
        List of all available functions.
        Needed to distinguish between parameters, features and functions.
    sanitizer : callable, optional
        Function for sanitizing variable names.
    env : dict, optional
        An environment to capture in the lambda's closure.

    Returns
    -------
    func : callable
        The created lambda function.
    params : list of str
        The recognied parameters withint the expression.
    vars_ : list of str
        The recognied variables withint the expression.

    Examples
    --------
    >>> from Orange.data import Table
    >>> data = Table("housing")
    >>> sfun = "a * exp(-b * CRIM * LSTAT) + c"
    >>> names = [a.name for a in data.domain.attributes]
    >>> func, par, var = _create_lambda(sfun, available_feature_names=names,
    ...                                 functions=["exp"], env={"exp": np.exp})
    >>> y = func(data.X, 1, 2, 3)
    >>> par
    ['a', 'b', 'c']
    >>> var
    ['CRIM', 'LSTAT']

    """
    if sanitizer is None:
        sanitizer = lambda n: n
    if env is None:
        env = {name: getattr(np, name) for name in functions}

    exp = ast.parse(expression, mode="eval")
    search = _ParametersSearch(
        [sanitizer(name) for name in available_feature_names],
        functions
    )
    search.visit(exp)
    params = search.parameters
    used_sanitized_feature_names = search.variables

    name = get_unique_names(params, "x")
    feature_mapper = {n: i for i, n in enumerate(used_sanitized_feature_names)}
    exp = _ReplaceVars(name, feature_mapper, functions).visit(exp)

    lambda_ = ast.Lambda(
        args=ast.arguments(
            posonlyargs=[],
            args=[ast.arg(arg=arg) for arg in [name] + params],
            varargs=None,
            kwonlyargs=[],
            kw_defaults=[],
            defaults=[],
        ),
        body=exp.body
    )
    exp = ast.Expression(body=lambda_)
    ast.fix_missing_locations(exp)
    vars_ = [name for name in available_feature_names
             if sanitizer(name) in used_sanitized_feature_names]

    # pylint: disable=eval-used
    return eval(compile(exp, "<lambda>", mode="eval"), env), params, vars_
def _create_lambda ( expression : Union [ str , ast . Expression ] = "" , available_feature_names : List [ str ] = None , functions : List [ str ] = None , sanitizer : Callable = None , env : Optional [ Dict [ str , Any ] ] = None ) -> Tuple [ Callable , List [ str ] , List [ str ] ] : if sanitizer is None : sanitizer = lambda n : n if env is None : env = { name : getattr ( np , name ) for name in functions } exp = ast . parse ( expression , mode = "eval" ) search = _ParametersSearch ( [ sanitizer ( name ) for name in available_feature_names ] , functions ) search . visit ( exp ) params = search . parameters used_sanitized_feature_names = search . variables name = get_unique_names ( params , "x" ) feature_mapper = { n : i for i , n in enumerate ( used_sanitized_feature_names ) } exp = _ReplaceVars ( name , feature_mapper , functions ) . visit ( exp ) lambda_ = ast . Lambda ( args = ast . arguments ( posonlyargs = [ ] , args = [ ast . arg ( arg = arg ) for arg in [ name ] + params ] , varargs = None , kwonlyargs = [ ] , kw_defaults = [ ] , defaults = [ ] , ) , body = exp . body ) exp = ast . Expression ( body = lambda_ ) ast . fix_missing_locations ( exp ) vars_ = [ name for name in available_feature_names if sanitizer ( name ) in used_sanitized_feature_names ] # pylint: disable=eval-used return eval ( compile ( exp , "<lambda>" , mode = "eval" ) , env ) , params , vars_