diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index 3cfec23..8588fde 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -12,13 +12,13 @@ Expression """ from functools import partial from ..core import AssocialTree, Tree, compute, typing, TypingError -from ..core.random import ( - extract_rdleaf, - extract_rv, - random_generator, - compute_leafs, - replace_rdleaf, -) +#from ..core.random import ( +# extract_rdleaf, +# extract_rv, +# random_generator, +# compute_leafs, +# replace_rdleaf, +#) from ..core.MO import moify from .tokens import factory from .renders import render diff --git a/mapytex/calculus/API/tokens/number.py b/mapytex/calculus/API/tokens/number.py index 7e0abb9..9181e27 100644 --- a/mapytex/calculus/API/tokens/number.py +++ b/mapytex/calculus/API/tokens/number.py @@ -13,7 +13,7 @@ Tokens representing interger and decimal from decimal import Decimal as _Decimal from .token import Token from ...core.arithmetic import gcd -from ...core.random.int_gene import filter_random +#from ...core.random.int_gene import filter_random from ...core.MO import MO, MOnumber from ...core.MO.fraction import MOFraction from random import random diff --git a/mapytex/calculus/__init__.py b/mapytex/calculus/__init__.py index 2566253..73d409f 100644 --- a/mapytex/calculus/__init__.py +++ b/mapytex/calculus/__init__.py @@ -31,7 +31,7 @@ Expression is the classe wich handle all calculus. It can randomly generate or i """ from .API import Expression, Integer, Decimal, render, Polynomial, Fraction -from .core import random_list +#from .core import random_list from decimal import getcontext #getcontext().prec = 2 diff --git a/mapytex/calculus/core/__init__.py b/mapytex/calculus/core/__init__.py index 41e8ad4..1e8d6c5 100644 --- a/mapytex/calculus/core/__init__.py +++ b/mapytex/calculus/core/__init__.py @@ -66,7 +66,6 @@ from .tree import Tree, AssocialTree from .compute import compute from .typing import typing, TypingError from .renders import tree2txt, tree2tex -from .random import list_generator as random_list # ----------------------------- diff --git a/mapytex/calculus/core/str2.py b/mapytex/calculus/core/str2.py index e0f6d23..edeace7 100644 --- a/mapytex/calculus/core/str2.py +++ b/mapytex/calculus/core/str2.py @@ -15,7 +15,6 @@ from decimal import Decimal, InvalidOperation from .coroutine import * from .operator import is_operator from .MO import moify_cor -from .random.leaf import look_for_rdleaf, RdLeaf __all__ = ["str2"] @@ -810,38 +809,7 @@ def str2(sink, convert_to_mo=True): return pipeline - -def rdstr2(sink): - """ Return a pipeline which parse random expression and with sink as endpoint - - :example: - >>> rdstr2list = rdstr2(list_sink) - >>> rdstr2list("{a}+{a*b}-2") - [, '+', , '+', ] - >>> rdstr2list("{a}({b}x+{c})") - [, '*', [, '*', , '+', ]] - """ - lfop = lookfor(is_operator) - operator_corout = partial(concurent_broadcast, lookfors=[lfop]) - - def pipeline(expression): - str2_corout = look_for_rdleaf( - lookforNumbers(operator_corout( - missing_times(moify_cor(pparser(sink))))) - ) - - for i in expression.replace(" ", ""): - str2_corout.send(i) - a = str2_corout.throw(STOOOP) - - return a - - return pipeline - - str2nestedlist = str2(list_sink) - - # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: diff --git a/mapytex/calculus/core/tree.py b/mapytex/calculus/core/tree.py index 090cab0..d1d379d 100644 --- a/mapytex/calculus/core/tree.py +++ b/mapytex/calculus/core/tree.py @@ -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, rdstr2 +from .str2 import str2 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, random=False): + def from_str(cls, expression, convert_to_mo=True): """ Initiate a tree from an string expression :example: @@ -77,26 +77,8 @@ class Tree: > * | > 3 | > n - >>> t = Tree.from_str("2+{n}x", random=True) - >>> print(t) - + - > 2 - > * - | > {n} - | > x - >>> t = Tree.from_str("{a}({b}x+{c})", random=True) - >>> print(t) - * - > {a} - > + - | > * - | | > {b} - | | > x - | > {c} - - """ - t = MutableTree.from_str(expression, convert_to_mo, random) + t = MutableTree.from_str(expression, convert_to_mo) return cls.from_any_tree(t) @classmethod @@ -907,7 +889,7 @@ class MutableTree(Tree): self.right_value = right_value @classmethod - def from_str(cls, expression, convert_to_mo=True, random=False): + def from_str(cls, expression, convert_to_mo=True): """ Initiate the MutableTree :example: @@ -963,29 +945,9 @@ class MutableTree(Tree): | | > 8 | | > 3 | > x - >>> t = MutableTree.from_str("{b}*x+{c}", random=True) - >>> print(t) - + - > * - | > {b} - | > x - > {c} - >>> t = MutableTree.from_str("{a}*({b}*x+{c})", random=True) - >>> print(t) - * - > {a} - > + - | > * - | | > {b} - | | > x - | > {c} """ - 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) + str_2_mut_tree = str2(cls.sink, convert_to_mo) + return str_2_mut_tree(expression) @classmethod @coroutine diff --git a/mapytex/calculus/random/__init__.py b/mapytex/calculus/random/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mapytex/calculus/core/random/__init__.py b/mapytex/calculus/random/core/__init__.py similarity index 97% rename from mapytex/calculus/core/random/__init__.py rename to mapytex/calculus/random/core/__init__.py index da8f50f..9fe4275 100644 --- a/mapytex/calculus/core/random/__init__.py +++ b/mapytex/calculus/random/core/__init__.py @@ -37,7 +37,8 @@ Tree with RdLeaf replaced by generated values :example: ->>> from ..tree import Tree +>>> from ...core.tree import Tree +>>> from .leaf import RdLeaf >>> rd_t = Tree("/", RdLeaf("a"), RdLeaf("a*k")) >>> print(rd_t) / @@ -74,16 +75,16 @@ This function ignores tree structure and works with lists __all__ = ["list_generator"] from random import choice -from functools import reduce -from .leaf import RdLeaf def extract_rdleaf(tree): """ Extract rdLeaf in a Tree :example: - >>> from ..tree import Tree + >>> from ...core.tree import Tree + >>> from .leaf import RdLeaf >>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k")) + >>> from .leaf import RdLeaf >>> extract_rdleaf(rd_t) ['a', 'a*k'] >>> rd_t = Tree("+", RdLeaf("a"), 2) @@ -139,7 +140,8 @@ def compute_leafs(leafs, generated_values): def replace_rdleaf(tree, computed_leafs): """ Replace RdLeaf by the corresponding computed value - >>> from ..tree import Tree + >>> from ...core.tree import Tree + >>> from .leaf import RdLeaf >>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k")) >>> computed_leafs = {'a': 2, 'a*k': 6} >>> print(replace_rdleaf(rd_t, computed_leafs)) diff --git a/mapytex/calculus/core/random/int_gene.py b/mapytex/calculus/random/core/int_gene.py similarity index 100% rename from mapytex/calculus/core/random/int_gene.py rename to mapytex/calculus/random/core/int_gene.py diff --git a/mapytex/calculus/core/random/leaf.py b/mapytex/calculus/random/core/leaf.py similarity index 94% rename from mapytex/calculus/core/random/leaf.py rename to mapytex/calculus/random/core/leaf.py index 471ed4b..7be0e65 100644 --- a/mapytex/calculus/core/random/leaf.py +++ b/mapytex/calculus/random/core/leaf.py @@ -9,7 +9,7 @@ """ """ -from ..coroutine import * +from ...core.coroutine import coroutine, STOOOP @coroutine @@ -17,7 +17,7 @@ def look_for_rdleaf(target): """ Coroutine which look to "{...}" which are RdLeaf :example: - >>> from ..str2 import list_sink + >>> from ...core.str2 import list_sink >>> str2list = look_for_rdleaf(list_sink) >>> for i in "{a}+{a*b}-2": ... str2list.send(i) diff --git a/mapytex/calculus/random/core/str2.py b/mapytex/calculus/random/core/str2.py new file mode 100644 index 0000000..92d3468 --- /dev/null +++ b/mapytex/calculus/random/core/str2.py @@ -0,0 +1,65 @@ + +from ...core.operator import is_operator +from functools import partial +from ...core.str2 import concurent_broadcast, lookforNumbers, pparser, missing_times, lookfor +from ...core.coroutine import STOOOP +from ...core.MO import moify_cor +from ...core.tree import MutableTree +from .leaf import look_for_rdleaf + +def rdstr2(sink): + """ Return a pipeline which parse random expression and with sink as endpoint + + :example: + >>> from ...core.str2 import list_sink + >>> rdstr2list = rdstr2(list_sink) + >>> rdstr2list("{a}+{a*b}-2") + [, '+', , '+', ] + >>> rdstr2list("{a}({b}x+{c})") + [, '*', [, '*', , '+', ]] + """ + lfop = lookfor(is_operator) + operator_corout = partial(concurent_broadcast, lookfors=[lfop]) + + def pipeline(expression): + str2_corout = look_for_rdleaf( + lookforNumbers(operator_corout( + missing_times(moify_cor(pparser(sink))))) + ) + + for i in expression.replace(" ", ""): + str2_corout.send(i) + a = str2_corout.throw(STOOOP) + + return a + + return pipeline + +class RandomTree(MutableTree): + """ MutableTree that accept {a} syntax for random generation """ + + @classmethod + def from_str(cls, expression): + """ Initiate a random tree from a string that need to be parsed + + :exemple: + >>> t = RandomTree.from_str("{b}*x+{c}") + >>> print(t) + + + > * + | > {b} + | > x + > {c} + >>> t = RandomTree.from_str("{a}*({b}*x+{c})") + >>> print(t) + * + > {a} + > + + | > * + | | > {b} + | | > x + | > {c} + """ + str_2_mut_tree = rdstr2(cls.sink) + return str_2_mut_tree(expression) +