2021-10-03 13:36:35 +00:00
|
|
|
"""
|
|
|
|
List generator
|
|
|
|
--------------
|
|
|
|
|
|
|
|
This function ignores tree structure and works with lists
|
|
|
|
|
|
|
|
>>> values = list_generator(["a", "a*b", "b", "c"], conditions=["b%c==1"])
|
|
|
|
>>> values # doctest: +SKIP
|
|
|
|
{'a': -8, 'a*b': -40, 'b': 5, 'c': 4}
|
|
|
|
"""
|
|
|
|
|
2021-10-03 13:41:54 +00:00
|
|
|
from .core.generate import random_generator
|
|
|
|
from .core.grammar import extract_letters, eval_words
|
2021-10-03 13:36:35 +00:00
|
|
|
|
2021-10-03 14:52:54 +00:00
|
|
|
DEFAUTL_CONFIG = {
|
|
|
|
"rejected": [0],
|
|
|
|
"min_max": (-10, 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
def list_generator(var_list:list[str], conditions:list[str]=[], global_config:dict={}, configs:dict={})->list[int]:
|
2021-10-03 13:36:35 +00:00
|
|
|
""" Generate random computed values from the list
|
|
|
|
|
|
|
|
:param rd_variables: list of random variables to generate (can be computed value - "a*b")
|
|
|
|
:param conditions: condition over variables
|
2021-10-06 14:17:50 +00:00
|
|
|
:param global_config: configuration for all variables
|
|
|
|
:param configs: configuration for each variables
|
|
|
|
:return: list of generated variables
|
2021-10-03 13:36:35 +00:00
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"])
|
|
|
|
>>> a, ab, b, c # doctest: +SKIP
|
|
|
|
(5, -20, -4, -3)
|
|
|
|
>>> a * b == ab
|
|
|
|
True
|
|
|
|
>>> ab # doctest: +SKIP
|
|
|
|
-20
|
|
|
|
>>> a, b # doctest: +SKIP
|
|
|
|
5, -4
|
2021-10-03 14:52:54 +00:00
|
|
|
>>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"], conditions=["a-b==0"])
|
|
|
|
>>> a - b == 0
|
|
|
|
True
|
|
|
|
>>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"], global_config={"rejected": [2, 3, 5, 7]})
|
|
|
|
>>> a not in [2, 3, 5, 7]
|
|
|
|
True
|
|
|
|
>>> b not in [2, 3, 5, 7]
|
|
|
|
True
|
|
|
|
>>> c not in [2, 3, 5, 7]
|
|
|
|
True
|
2021-10-06 13:41:35 +00:00
|
|
|
>>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"], configs={"a": {"rejected": [2, 3, 5, 7]}})
|
2021-10-03 14:52:54 +00:00
|
|
|
>>> a not in [2, 3, 5, 7]
|
|
|
|
True
|
2021-10-03 13:36:35 +00:00
|
|
|
"""
|
|
|
|
rv = extract_letters(var_list)
|
2021-10-06 13:41:35 +00:00
|
|
|
rv_gen = random_generator(rv, conditions, dict(DEFAUTL_CONFIG, **global_config), configs)
|
2021-10-03 13:36:35 +00:00
|
|
|
generated = eval_words(var_list, rv_gen)
|
|
|
|
return [generated[v] for v in var_list]
|