mirror of
https://github.com/Faraphel/Atlas-Install.git
synced 2025-07-05 20:28:27 +02:00
safe_eval now cache return_lambda=True callable
This commit is contained in:
parent
6ecf752b6d
commit
89de19c723
1 changed files with 12 additions and 1 deletions
|
@ -9,6 +9,10 @@ if TYPE_CHECKING:
|
||||||
from source import TemplateSafeEval, Env
|
from source import TemplateSafeEval, Env
|
||||||
|
|
||||||
|
|
||||||
|
self = __import__(__name__)
|
||||||
|
self.safe_eval_cache = {}
|
||||||
|
|
||||||
|
|
||||||
class SafeEvalException(Exception):
|
class SafeEvalException(Exception):
|
||||||
def __init__(self, message: str):
|
def __init__(self, message: str):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
@ -38,6 +42,11 @@ def safe_eval(template: "TemplateSafeEval", env: "Env" = None, macros: dict[str,
|
||||||
if len(template) == 0: return ""
|
if len(template) == 0: return ""
|
||||||
if env is None: env = {}
|
if env is None: env = {}
|
||||||
if macros is None: macros = {}
|
if macros is None: macros = {}
|
||||||
|
if lambda_args is None: lambda_args = []
|
||||||
|
|
||||||
|
# if the safe_eval return a callable and have already been called, return the cached callable
|
||||||
|
if return_lambda is True and template in self.safe_eval_cache:
|
||||||
|
return self.safe_eval_cache[template]
|
||||||
|
|
||||||
# replace the macro in the template
|
# replace the macro in the template
|
||||||
template = replace_macro(template=template, macros=macros)
|
template = replace_macro(template=template, macros=macros)
|
||||||
|
@ -122,4 +131,6 @@ def safe_eval(template: "TemplateSafeEval", env: "Env" = None, macros: dict[str,
|
||||||
ast.fix_missing_locations(expression)
|
ast.fix_missing_locations(expression)
|
||||||
|
|
||||||
# return the evaluated formula
|
# return the evaluated formula
|
||||||
return eval(compile(expression, "<safe_eval>", "eval"), globals_, locals_)
|
result = eval(compile(expression, "<safe_eval>", "eval"), globals_, locals_)
|
||||||
|
if return_lambda: self.safe_eval_cache[template] = result # cache the callable for potential latter call
|
||||||
|
return result
|
||||||
|
|
Loading…
Reference in a new issue