Feat(random): random tree generation flow is ok. Need to create generate

function
This commit is contained in:
Bertrand Benjamin 2019-05-09 18:37:39 +02:00
parent a77c2f9795
commit c7561fa6fe
2 changed files with 111 additions and 20 deletions

View File

@ -7,7 +7,7 @@
# Distributed under terms of the MIT license.
"""
Tools to extract random nodes, random variables, generate random values and
Tools to extract random leafs, random variables, generate random values and
fill new trees
Flow
@ -15,9 +15,9 @@ Flow
Tree with RdLeaf
|
| Extract nodes
| Extract rdLeaf
|
List of nodes to generate
List of leafs to generate
|
| extract_rv
|
@ -27,41 +27,100 @@ List random variables to generate
|
Dictionnary of generated random variables
|
| Compute nodes
| Compute leafs
|
Dictionnary of computed nodes
Dictionnary of computed leafs
|
| Replace
|
Tree with RdLeaf replaced by generated values
:example:
>>> from ..tree import Tree
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
>>> print(rd_t)
+
> {a}
> {a*k}
>>> extract_rdleaf(rd_t)
['a', 'a*k']
"""
from .leaf import RdLeaf
def extract_rv(node_list):
""" Extract the set of random values from the node list
def extract_rdleaf(tree):
""" Extract rdLeaf in a Tree
:param node_list: list of nodes
:example:
>>> from ..tree import Tree
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
>>> extract_rdleaf(rd_t)
['a', 'a*k']
>>> rd_t = Tree("+", RdLeaf("a"), 2)
>>> extract_rdleaf(rd_t)
['a']
"""
rd_leafs = []
for leaf in tree.get_leafs():
try:
leaf.rdleaf
except AttributeError:
pass
else:
rd_leafs.append(leaf.name)
return rd_leafs
def extract_rv(leafs):
""" Extract the set of random values from the leaf list
:param leafs: list of leafs
:return: set of random values
:example:
>>> node_list = ["{a}", "{a*k}"]
>>> extract_rv(node_list)
{"a", "k"}
>>> leafs = ["a", "a*k"]
>>> list(extract_rv(leafs))
['a', 'k']
"""
pass
rd_values = set()
for leaf in leafs:
for c in leaf:
if c.isalpha():
rd_values.add(c)
return rd_values
def compute_nodes(node_list, generated_values):
""" Compute nodes from generated random values
def compute_leafs(leafs, generated_values):
""" Compute leafs from generated random values
:param generated_values: Dictionnary of name:generated value
:param node_list: list of nodes
:return: Dictionnary of evaluated nodes from generated values
:param leafs: list of leafs
:return: Dictionnary of evaluated leafs from generated values
:example:
>>> node_list = ["{a}", "{a*k}"]
>>> leafs = ["a", "a*k"]
>>> generated_values = {"a":2, "k":3}
>>> compute_nodes(node_list, generated_values)
{"{a}": 2, "{a*k}": 6}
>>> compute_leafs(leafs, generated_values)
{'a': 2, 'a*k': 6}
"""
pass
return {leaf: eval(leaf, generated_values) for leaf in leafs}
def replace_rdleaf(tree, computed_leafs):
""" Replace RdLeaf by the corresponding computed value
>>> from ..tree import Tree
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
>>> computed_leafs = {'a': 2, 'a*k': 6}
>>> print(replace_rdleaf(rd_t, computed_leafs))
+
> 2
> 6
"""
def replace(leaf):
try:
return leaf.replace(computed_leafs)
except AttributeError:
return leaf
return tree.map_on_leaf(replace)

View File

@ -0,0 +1,32 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
"""
class RdLeaf():
""" Random leaf
"""
def __init__(self, name):
self._name = name
self.rdleaf = True
@property
def name(self):
return self._name
def replace(self, computed):
return computed[self.name]
def __str__(self):
return "{" + self.name + "}"
def __repr__(self):
return f"<{self.__class__.__name__} {self.__str__}>"