Doc: Doctest for Tree generator

This commit is contained in:
Bertrand Benjamin 2019-05-12 17:14:25 +02:00
parent 295677045b
commit 33ded0d90e

View File

@ -38,18 +38,34 @@ 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
def extract_rdleaf(tree): def extract_rdleaf(tree):
@ -125,7 +141,6 @@ def replace_rdleaf(tree, computed_leafs):
return leaf return leaf
return tree.map_on_leaf(replace) return tree.map_on_leaf(replace)
def random_generator(rd_variables, def random_generator(rd_variables,
conditions = [], conditions = [],
rejected = [0], rejected = [0],
@ -170,6 +185,10 @@ def random_generator(rd_variables,
).difference(complete_scope[v]["rejected"])) ).difference(complete_scope[v]["rejected"]))
for v in rd_variables} 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}
while not all([eval(c, __builtins__, generate_variable) for c in conditions]): while not all([eval(c, __builtins__, generate_variable) for c in conditions]):