Feat: Expression can be generated randomly!!!
This commit is contained in:
		| @@ -31,6 +31,7 @@ def look_for_rdleaf(target): | ||||
|     except TypeError: | ||||
|         target_ = target | ||||
|  | ||||
|     stacking = False | ||||
|     try: | ||||
|         while True: | ||||
|             tok = yield | ||||
|   | ||||
| @@ -11,7 +11,7 @@ Tree class | ||||
|  | ||||
| from .tree_tools import to_nested_parenthesis, postfix_concatenate, show_tree | ||||
| from .coroutine import coroutine, STOOOP | ||||
| from .str2 import str2 | ||||
| from .str2 import str2, rdstr2 | ||||
| from .operator import OPERATORS, is_operator | ||||
|  | ||||
| __all__ = ["Tree", "MutableTree"] | ||||
| @@ -51,7 +51,7 @@ class Tree: | ||||
|         self.right_value = right_value | ||||
|  | ||||
|     @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 | ||||
|  | ||||
|         :example: | ||||
| @@ -77,9 +77,17 @@ class Tree: | ||||
|          > * | ||||
|          | > 3 | ||||
|          | > 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) | ||||
|  | ||||
|     @classmethod | ||||
| @@ -890,7 +898,7 @@ class MutableTree(Tree): | ||||
|         self.right_value = right_value | ||||
|  | ||||
|     @classmethod | ||||
|     def from_str(cls, expression, convert_to_mo=True): | ||||
|     def from_str(cls, expression, convert_to_mo=True, random=False): | ||||
|         """ Initiate the MutableTree | ||||
|  | ||||
|         :example: | ||||
| @@ -948,8 +956,12 @@ class MutableTree(Tree): | ||||
|          | > x | ||||
|  | ||||
|         """ | ||||
|         str_2_mut_tree = str2(cls.sink, convert_to_mo) | ||||
|         return str_2_mut_tree(expression) | ||||
|         if random: | ||||
|             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 | ||||
|     @coroutine | ||||
|   | ||||
		Reference in New Issue
	
	Block a user