Feat: use functions from math module in variable and conditions
This commit is contained in:
@@ -8,6 +8,10 @@
|
||||
|
||||
|
||||
from random import choice
|
||||
import math
|
||||
|
||||
EVAL_FUN = {**math.__dict__}
|
||||
|
||||
|
||||
|
||||
def complete_variable_configs(
|
||||
@@ -62,6 +66,8 @@ def random_generator(
|
||||
:param configs: global parameters
|
||||
:return: dictionnary of generated variables
|
||||
|
||||
In variables and configurations, you have access to all math module functions
|
||||
|
||||
:example:
|
||||
>>> gene = random_generator(["a", "b"],
|
||||
... ["a > 0"],
|
||||
@@ -104,7 +110,7 @@ def random_generator(
|
||||
|
||||
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
||||
|
||||
while not all([eval(c, __builtins__, generate_variable) for c in conditions]):
|
||||
while not all([eval(c, EVAL_FUN, generate_variable) for c in conditions]):
|
||||
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
||||
|
||||
return generate_variable
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import math
|
||||
|
||||
EVAL_FUN = {**math.__dict__}
|
||||
|
||||
def extract_letters(words: list[str]) -> set[str]:
|
||||
"""Extracts unique letters from a list of words
|
||||
|
||||
@@ -24,10 +28,16 @@ def eval_words(words: list[str], values: dict[str, int]) -> dict[str, int]:
|
||||
:param values: Dictionary of letters:value
|
||||
:return: Dictionary of evaluated words from generated values
|
||||
|
||||
In words, you have access to all math module functions
|
||||
|
||||
:example:
|
||||
>>> leafs = ["a", "a*k"]
|
||||
>>> generated_values = {"a":2, "k":3}
|
||||
>>> eval_words(leafs, generated_values)
|
||||
{'a': 2, 'a*k': 6}
|
||||
>>> leafs = ["exp(a)", "gcd(a, k)"]
|
||||
>>> generated_values = {"a":2, "k":3}
|
||||
>>> eval_words(leafs, generated_values)
|
||||
{'exp(a)': 7.38905609893065, 'gcd(a, k)': 1}
|
||||
"""
|
||||
return {word: eval(word, values) for word in words}
|
||||
return {word: eval(word, EVAL_FUN, values) for word in words}
|
||||
|
||||
Reference in New Issue
Block a user