Doc: Doctest for Tree generator
This commit is contained in:
parent
295677045b
commit
33ded0d90e
@ -38,18 +38,34 @@ Tree with RdLeaf replaced by generated values
|
||||
:example:
|
||||
|
||||
>>> from ..tree import Tree
|
||||
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
|
||||
>>> rd_t = Tree("/", RdLeaf("a"), RdLeaf("a*k"))
|
||||
>>> print(rd_t)
|
||||
+
|
||||
/
|
||||
> {a}
|
||||
> {a*k}
|
||||
>>> extract_rdleaf(rd_t)
|
||||
>>> leafs = extract_rdleaf(rd_t)
|
||||
>>> leafs
|
||||
['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 functools import reduce
|
||||
from .leaf import RdLeaf
|
||||
|
||||
def extract_rdleaf(tree):
|
||||
@ -125,7 +141,6 @@ def replace_rdleaf(tree, computed_leafs):
|
||||
return leaf
|
||||
return tree.map_on_leaf(replace)
|
||||
|
||||
|
||||
def random_generator(rd_variables,
|
||||
conditions = [],
|
||||
rejected = [0],
|
||||
@ -170,6 +185,10 @@ def random_generator(rd_variables,
|
||||
).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}
|
||||
|
||||
while not all([eval(c, __builtins__, generate_variable) for c in conditions]):
|
||||
|
Loading…
Reference in New Issue
Block a user