Feat: move extract_letters and eval_words in grammar

This commit is contained in:
Bertrand Benjamin 2021-10-03 15:41:54 +02:00
parent 204c7dffd7
commit b2b204c17b
5 changed files with 40 additions and 38 deletions

View File

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

View File

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

View File

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

View File

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

View File

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