From b738cf8dd8ca48558caeeafae5c95aad4baa90e7 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sat, 9 Oct 2021 16:09:09 +0200 Subject: [PATCH] Feat: use functions from math module in variable and conditions --- mapytex/calculus/random/core/generate.py | 8 ++++- mapytex/calculus/random/core/grammar.py | 12 +++++++- test/calculus/random/test_list_generator.py | 34 ++++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/mapytex/calculus/random/core/generate.py b/mapytex/calculus/random/core/generate.py index 70cddf8..143ed36 100644 --- a/mapytex/calculus/random/core/generate.py +++ b/mapytex/calculus/random/core/generate.py @@ -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 diff --git a/mapytex/calculus/random/core/grammar.py b/mapytex/calculus/random/core/grammar.py index be5a462..c36625b 100644 --- a/mapytex/calculus/random/core/grammar.py +++ b/mapytex/calculus/random/core/grammar.py @@ -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} diff --git a/test/calculus/random/test_list_generator.py b/test/calculus/random/test_list_generator.py index 8b7bf0d..f4bf747 100644 --- a/test/calculus/random/test_list_generator.py +++ b/test/calculus/random/test_list_generator.py @@ -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"], ) +