Refact: move random function away from core

This commit is contained in:
Bertrand Benjamin 2021-09-30 14:52:08 +02:00
parent daed07efa3
commit 1672530179
11 changed files with 89 additions and 93 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
# -----------------------------

View File

@ -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")
[<RdLeaf a>, '+', <RdLeaf a*b>, '+', <MOnumber -2>]
>>> rdstr2list("{a}({b}x+{c})")
[<RdLeaf a>, '*', [<RdLeaf b>, '*', <MOstr x>, '+', <RdLeaf 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:

View File

@ -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,27 +945,7 @@ 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)

View File

View File

@ -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))

View File

@ -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)

View File

@ -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")
[<RdLeaf a>, '+', <RdLeaf a*b>, '+', <MOnumber -2>]
>>> rdstr2list("{a}({b}x+{c})")
[<RdLeaf a>, '*', [<RdLeaf b>, '*', <MOstr x>, '+', <RdLeaf 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)