Feat: Expression can be generated randomly!!!
This commit is contained in:
		| @@ -11,6 +11,8 @@ Expression | |||||||
|  |  | ||||||
| """ | """ | ||||||
| from ..core import AssocialTree, Tree, compute, typing, TypingError | from ..core import AssocialTree, Tree, compute, typing, TypingError | ||||||
|  | from ..core.random import extract_rdleaf, extract_rv, random_generator, compute_leafs, replace_rdleaf | ||||||
|  | from ..core.MO import moify | ||||||
| from .tokens import factory | from .tokens import factory | ||||||
| from .renders import renders | from .renders import renders | ||||||
|  |  | ||||||
| @@ -68,25 +70,60 @@ class Expression(object): | |||||||
|         """ |         """ | ||||||
|         t = Tree.from_str(string) |         t = Tree.from_str(string) | ||||||
|         if typing: |         if typing: | ||||||
|             tt = cls(t)._typing() |             return cls._post_precessing(t) | ||||||
|             try: |  | ||||||
|                 return factory(tt) |  | ||||||
|             except TypeError as e: |  | ||||||
|                 return cls(t) |  | ||||||
|  |  | ||||||
|         return cls(t) |         return cls(t) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def random(self, template, conditions=[], shuffle=False): |     def random( | ||||||
|         """ Initiate randomly the expression  |         cls, | ||||||
|  |         template, | ||||||
|  |         conditions=[], | ||||||
|  |         rejected=[0], | ||||||
|  |         min_max=(-10, 10), | ||||||
|  |         variables_scope={}, | ||||||
|  |         shuffle=False, | ||||||
|  |     ): | ||||||
|  |         """ Initiate randomly the expression | ||||||
|  |  | ||||||
|         :param template: the template of the expression |         :param template: the template of the expression | ||||||
|         :param conditions: conditions on randomly generate variable |         :param conditions: conditions on randomly generate variable | ||||||
|  |         :param rejected: Values to reject for all random variables | ||||||
|  |         :param min_max: Min and max value for all random variables | ||||||
|  |         :param variables_scope: Dictionnary for each random varaibles to fic rejected and min_max | ||||||
|         :param shuffle: allowing to shuffle the tree |         :param shuffle: allowing to shuffle the tree | ||||||
|         :returns: TODO |         :returns: TODO | ||||||
|  |  | ||||||
|  |         :example: | ||||||
|  |         >>> Expression.random("{a}/{a*k}") # doctest: +SKIP | ||||||
|  |         <Exp: -3 / -15> | ||||||
|  |         >>> Expression.random("{a}/{a*k} - 3*{b}", variables_scope={'a':{'min_max':(10, 30)}}) | ||||||
|  |         <Exp: 18 / 108 - 3 * 9> | ||||||
|  |  | ||||||
|         """ |         """ | ||||||
|         pass |         rd_t = Tree.from_str(template, random=True) | ||||||
|  |         leafs = extract_rdleaf(rd_t) | ||||||
|  |         rd_varia = extract_rv(leafs) | ||||||
|  |         generated = random_generator( | ||||||
|  |             rd_varia, conditions, rejected, min_max, variables_scope | ||||||
|  |         ) | ||||||
|  |         computed = compute_leafs(leafs, generated) | ||||||
|  |         t = replace_rdleaf(rd_t, computed).map_on_leaf(moify) | ||||||
|  |         t = cls._post_precessing(t) | ||||||
|  |  | ||||||
|  |         if shuffle: | ||||||
|  |             raise NotImplemented("Can't suffle expression yet") | ||||||
|  |  | ||||||
|  |         return cls(t) | ||||||
|  |  | ||||||
|  |     @classmethod | ||||||
|  |     def _post_precessing(cls, t): | ||||||
|  |         """ Post process the tree by typing it """ | ||||||
|  |         tt = cls(t)._typing() | ||||||
|  |         try: | ||||||
|  |             return factory(tt) | ||||||
|  |         except TypeError as e: | ||||||
|  |             return cls(t) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def set_render(cls, render): |     def set_render(cls, render): | ||||||
|   | |||||||
| @@ -31,6 +31,7 @@ def look_for_rdleaf(target): | |||||||
|     except TypeError: |     except TypeError: | ||||||
|         target_ = target |         target_ = target | ||||||
|  |  | ||||||
|  |     stacking = False | ||||||
|     try: |     try: | ||||||
|         while True: |         while True: | ||||||
|             tok = yield |             tok = yield | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ Tree class | |||||||
|  |  | ||||||
| from .tree_tools import to_nested_parenthesis, postfix_concatenate, show_tree | from .tree_tools import to_nested_parenthesis, postfix_concatenate, show_tree | ||||||
| from .coroutine import coroutine, STOOOP | from .coroutine import coroutine, STOOOP | ||||||
| from .str2 import str2 | from .str2 import str2, rdstr2 | ||||||
| from .operator import OPERATORS, is_operator | from .operator import OPERATORS, is_operator | ||||||
|  |  | ||||||
| __all__ = ["Tree", "MutableTree"] | __all__ = ["Tree", "MutableTree"] | ||||||
| @@ -51,7 +51,7 @@ class Tree: | |||||||
|         self.right_value = right_value |         self.right_value = right_value | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def from_str(cls, expression, convert_to_mo=True): |     def from_str(cls, expression, convert_to_mo=True, random=False): | ||||||
|         """ Initiate a tree from an string expression |         """ Initiate a tree from an string expression | ||||||
|  |  | ||||||
|         :example: |         :example: | ||||||
| @@ -77,9 +77,17 @@ class Tree: | |||||||
|          > * |          > * | ||||||
|          | > 3 |          | > 3 | ||||||
|          | > n |          | > n | ||||||
|  |         >>> t = Tree.from_str("2+{n}*x", random=True) | ||||||
|  |         >>> print(t) | ||||||
|  |         + | ||||||
|  |          > 2 | ||||||
|  |          > * | ||||||
|  |          | > {n} | ||||||
|  |          | > x | ||||||
|  |  | ||||||
|  |  | ||||||
|         """ |         """ | ||||||
|         t = MutableTree.from_str(expression, convert_to_mo) |         t = MutableTree.from_str(expression, convert_to_mo, random) | ||||||
|         return cls.from_any_tree(t) |         return cls.from_any_tree(t) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
| @@ -890,7 +898,7 @@ class MutableTree(Tree): | |||||||
|         self.right_value = right_value |         self.right_value = right_value | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def from_str(cls, expression, convert_to_mo=True): |     def from_str(cls, expression, convert_to_mo=True, random=False): | ||||||
|         """ Initiate the MutableTree |         """ Initiate the MutableTree | ||||||
|  |  | ||||||
|         :example: |         :example: | ||||||
| @@ -948,8 +956,12 @@ class MutableTree(Tree): | |||||||
|          | > x |          | > x | ||||||
|  |  | ||||||
|         """ |         """ | ||||||
|         str_2_mut_tree = str2(cls.sink, convert_to_mo) |         if random: | ||||||
|         return str_2_mut_tree(expression) |             str_2_mut_tree = rdstr2(cls.sink) | ||||||
|  |             return str_2_mut_tree(expression) | ||||||
|  |         else: | ||||||
|  |             str_2_mut_tree = str2(cls.sink, convert_to_mo) | ||||||
|  |             return str_2_mut_tree(expression) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     @coroutine |     @coroutine | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user