From b2b204c17b2b6243a7d17965ec77f78c72ab5da4 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 3 Oct 2021 15:41:54 +0200 Subject: [PATCH] Feat: move extract_letters and eval_words in grammar --- mapytex/calculus/random/core/__init__.py | 3 +- mapytex/calculus/random/core/generate.py | 35 --------------------- mapytex/calculus/random/core/grammar.py | 35 +++++++++++++++++++++ mapytex/calculus/random/core/random_tree.py | 2 +- mapytex/calculus/random/list.py | 3 +- 5 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 mapytex/calculus/random/core/grammar.py diff --git a/mapytex/calculus/random/core/__init__.py b/mapytex/calculus/random/core/__init__.py index 4686114..895060e 100644 --- a/mapytex/calculus/random/core/__init__.py +++ b/mapytex/calculus/random/core/__init__.py @@ -39,7 +39,8 @@ Tree with RdLeaf replaced by generated values >>> from .random_tree import RandomTree >>> from .leaf import RdLeaf ->>> from .generate import extract_letters, random_generator, eval_words +>>> from .generate import random_generator +>>> from .grammar import extract_letters, eval_words >>> rd_t = RandomTree("/", RdLeaf("a"), RdLeaf("a*k")) >>> print(rd_t) / diff --git a/mapytex/calculus/random/core/generate.py b/mapytex/calculus/random/core/generate.py index b9557f2..4098aeb 100644 --- a/mapytex/calculus/random/core/generate.py +++ b/mapytex/calculus/random/core/generate.py @@ -10,41 +10,6 @@ from random import choice -def extract_letters(words:list[str])->set[str]: - """ Extracts unique letters from a list of words - - :param words: list of leafs - :return: set of letters - - :example: - >>> leafs = ["a", "a*k"] - >>> extract_letters(leafs) == {'a', 'k'} - True - """ - letters = set() - for word in words: - for c in word: - if c.isalpha(): - letters.add(c) - return letters - - -def eval_words(words:list[str], values:dict[str,int]) -> dict[str, int]: - """ Evaluate words replacing letters with values - - :param words: list of words - :param values: Dictionary of letters:value - :return: Dictionary of evaluated words from generated values - - :example: - >>> leafs = ["a", "a*k"] - >>> generated_values = {"a":2, "k":3} - >>> eval_words(leafs, generated_values) - {'a': 2, 'a*k': 6} - """ - return {word: eval(word, values) for word in words} - - def build_variable_scope(rd_variables, rejected, min_max, variables_scope): """ Build variables scope from incomplete one diff --git a/mapytex/calculus/random/core/grammar.py b/mapytex/calculus/random/core/grammar.py new file mode 100644 index 0000000..f1ec288 --- /dev/null +++ b/mapytex/calculus/random/core/grammar.py @@ -0,0 +1,35 @@ + +def extract_letters(words:list[str])->set[str]: + """ Extracts unique letters from a list of words + + :param words: list of leafs + :return: set of letters + + :example: + >>> leafs = ["a", "a*k"] + >>> extract_letters(leafs) == {'a', 'k'} + True + """ + letters = set() + for word in words: + for c in word: + if c.isalpha(): + letters.add(c) + return letters + + +def eval_words(words:list[str], values:dict[str,int]) -> dict[str, int]: + """ Evaluate words replacing letters with values + + :param words: list of words + :param values: Dictionary of letters:value + :return: Dictionary of evaluated words from generated values + + :example: + >>> leafs = ["a", "a*k"] + >>> generated_values = {"a":2, "k":3} + >>> eval_words(leafs, generated_values) + {'a': 2, 'a*k': 6} + """ + return {word: eval(word, values) for word in words} + diff --git a/mapytex/calculus/random/core/random_tree.py b/mapytex/calculus/random/core/random_tree.py index 599bfb0..68ddb08 100644 --- a/mapytex/calculus/random/core/random_tree.py +++ b/mapytex/calculus/random/core/random_tree.py @@ -1,5 +1,5 @@ from ...core.tree import MutableTree, Tree -from .generate import extract_letters, random_generator +from .grammar import extract_letters from .str2 import rdstr2 class RandomTree(MutableTree): diff --git a/mapytex/calculus/random/list.py b/mapytex/calculus/random/list.py index a92b428..c0ca10b 100644 --- a/mapytex/calculus/random/list.py +++ b/mapytex/calculus/random/list.py @@ -9,7 +9,8 @@ This function ignores tree structure and works with lists {'a': -8, 'a*b': -40, 'b': 5, 'c': 4} """ -from .core.generate import extract_letters, random_generator, eval_words +from .core.generate import random_generator +from .core.grammar import extract_letters, eval_words def list_generator(var_list, conditions=[], rejected=[0], min_max=(-10, 10), variables_scope={}, dictionnary=False): """ Generate random computed values from the list