Feat: use functions from math module in variable and conditions

This commit is contained in:
Bertrand Benjamin 2021-10-09 16:09:09 +02:00
parent 32112a4591
commit b738cf8dd8
3 changed files with 51 additions and 3 deletions

View File

@ -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

View File

@ -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}

View File

@ -1,4 +1,36 @@
import mapytex
def test_generate_list():
mapytex.random.list(["a", "b", "c"])
random_list = mapytex.random.list(["a", "b"])
assert len(random_list) == 2
random_list = mapytex.random.list(["a", "b", "c"])
assert len(random_list) == 3
random_list = mapytex.random.list(["a", "b", "a", "b"])
assert random_list[0] == random_list[2]
assert random_list[1] == random_list[3]
def test_generate_list_calculus():
random_list = mapytex.random.list(["a", "b", "a+b"])
assert random_list[0] + random_list[1] == random_list[2]
random_list = mapytex.random.list(["a", "b", "a-b"])
assert random_list[0] - random_list[1] == random_list[2]
random_list = mapytex.random.list(["a", "b", "a*b"])
assert random_list[0] * random_list[1] == random_list[2]
random_list = mapytex.random.list(["a", "b", "a/b"])
assert random_list[0] / random_list[1] == random_list[2]
def test_generate_list_calculus_math():
import math
a, b, gcd = mapytex.random.list(["a", "b", "gcd(a, b)"])
assert math.gcd(a, b) == gcd
a, b, exp, log = mapytex.random.list(["a", "b", "exp(a)", "log(b)"])
assert math.exp(a) == exp
assert math.log(b) == log
def test_generate_list_conditions():
random_list = mapytex.random.list(["a", "b"], )