Doc: Doctest for Tree generator

This commit is contained in:
Bertrand Benjamin 2019-05-12 17:14:25 +02:00
parent 4e7ba55970
commit 040dfff0ec

View File

@ -38,16 +38,33 @@ Tree with RdLeaf replaced by generated values
:example: :example:
>>> from ..tree import Tree >>> from ..tree import Tree
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k")) >>> rd_t = Tree("/", RdLeaf("a"), RdLeaf("a*k"))
>>> print(rd_t) >>> print(rd_t)
+ /
> {a} > {a}
> {a*k} > {a*k}
>>> extract_rdleaf(rd_t) >>> leafs = extract_rdleaf(rd_t)
>>> leafs
['a', 'a*k'] ['a', 'a*k']
>>> rd_varia = extract_rv(leafs)
>>> rd_varia # doctest: +SKIP
{'a', 'k'}
>>> generated = random_generator(rd_varia, conditions=['a%2+1'])
>>> generated # doctest: +SKIP
{'a': 7, 'k': 4}
>>> computed = compute_leafs(leafs, generated)
>>> computed # doctest: +SKIP
{'a': 7, 'a*k': 28}
>>> replaced = replace_rdleaf(rd_t, computed)
>>> print(replaced) # doctest: +SKIP
/
> 7
> 28
""" """
from random import choice from random import choice
from functools import reduce
from .leaf import RdLeaf from .leaf import RdLeaf
@ -129,11 +146,10 @@ def replace_rdleaf(tree, computed_leafs):
return tree.map_on_leaf(replace) return tree.map_on_leaf(replace)
def random_generator(rd_variables,
conditions = [], def random_generator(
rejected = [0], rd_variables, conditions=[], rejected=[0], min_max=(-10, 10), variables_scope={}
min_max = (-10, 10), ):
variables_scope = {}):
""" Generate random variables """ Generate random variables
:param rd_variables: list of random variables to generate :param rd_variables: list of random variables to generate
@ -165,13 +181,23 @@ def random_generator(rd_variables,
>>> gene["a"] % gene["b"] >>> gene["a"] % gene["b"]
0 0
""" """
complete_scope = build_variable_scope(rd_variables, complete_scope = build_variable_scope(
rejected, min_max, rd_variables, rejected, min_max, variables_scope
variables_scope) )
choices_list = {v: list(set(range( choices_list = {
complete_scope[v]["min_max"][0], complete_scope[v]["min_max"][1]+1) v: list(
).difference(complete_scope[v]["rejected"])) set(
for v in rd_variables} range(
complete_scope[v]["min_max"][0], complete_scope[v]["min_max"][1] + 1
)
).difference(complete_scope[v]["rejected"])
)
for v in rd_variables
}
# quantity_choices = reduce(lambda x,y : x*y,
# [len(choices_list[v]) for v in choices_list])
# TODO: améliorer la méthode de rejet avec un cache |dim. mai 12 17:04:11 CEST 2019
generate_variable = {v: choice(choices_list[v]) for v in rd_variables} generate_variable = {v: choice(choices_list[v]) for v in rd_variables}
@ -180,6 +206,7 @@ def random_generator(rd_variables,
return generate_variable return generate_variable
def build_variable_scope(rd_variables, rejected, min_max, variables_scope): def build_variable_scope(rd_variables, rejected, min_max, variables_scope):
""" Build variables scope from incomplete one """ Build variables scope from incomplete one