allow raw string generation
This commit is contained in:
parent
698fea5c63
commit
e669e9707d
@ -9,15 +9,17 @@ import re
|
|||||||
class RdExpression(object):
|
class RdExpression(object):
|
||||||
"""A generator of random expression builder"""
|
"""A generator of random expression builder"""
|
||||||
|
|
||||||
def __init__(self, form, conditions = []):
|
def __init__(self, form, conditions = [], with_Exp = True):
|
||||||
"""Initiate the generator
|
"""Initiate the generator
|
||||||
|
|
||||||
:param form: the form of the expression (/!\ variables need to be in brackets {})
|
:param form: the form of the expression (/!\ variables need to be in brackets {})
|
||||||
:param conditions: condition on variables (/!\ variables need to be in brackets {})
|
:param conditions: condition on variables (/!\ variables need to be in brackets {})
|
||||||
|
:param with_Exp: If True, __call__ return an expression rendered through Expression class. If False, keep form and replace inside.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self._form = form
|
self._form = form
|
||||||
self._conditions = conditions
|
self._conditions = conditions
|
||||||
|
self._with_Exp = with_Exp
|
||||||
self._letters = self.get_letters()
|
self._letters = self.get_letters()
|
||||||
self._gene_varia = {}
|
self._gene_varia = {}
|
||||||
self._gene_2replaced= {}
|
self._gene_2replaced= {}
|
||||||
@ -59,10 +61,13 @@ class RdExpression(object):
|
|||||||
:returns: an formated random expression
|
:returns: an formated random expression
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if self._with_Exp:
|
||||||
return render(self.raw_exp(val_min, val_max).postfix_tokens)
|
return render(self.raw_exp(val_min, val_max).postfix_tokens)
|
||||||
|
else:
|
||||||
|
return self.raw_str(val_min, val_max)
|
||||||
|
|
||||||
def raw_exp(self, val_min = -10, val_max = 10):
|
def raw_str(self, val_min = -10, val_max = 10):
|
||||||
"""Same as __call_ but returns an Expression object
|
"""Return raw string (don't use Expression for rendering or parsing)
|
||||||
|
|
||||||
:param val_min: minimum value random generation
|
:param val_min: minimum value random generation
|
||||||
:param val_max: maximum value random generation
|
:param val_max: maximum value random generation
|
||||||
@ -76,6 +81,18 @@ class RdExpression(object):
|
|||||||
|
|
||||||
exp = self._form.format(**self._gene_2replaced)
|
exp = self._form.format(**self._gene_2replaced)
|
||||||
|
|
||||||
|
return exp
|
||||||
|
|
||||||
|
def raw_exp(self, val_min = -10, val_max = 10):
|
||||||
|
"""Same as raw_str but returns an Expression object
|
||||||
|
|
||||||
|
:param val_min: minimum value random generation
|
||||||
|
:param val_max: maximum value random generation
|
||||||
|
:returns: an random Expression object
|
||||||
|
|
||||||
|
"""
|
||||||
|
exp = self.raw_str(val_min, val_max)
|
||||||
|
|
||||||
return Expression(exp)
|
return Expression(exp)
|
||||||
|
|
||||||
def gene_varia(self, val_min = -10, val_max = 10):
|
def gene_varia(self, val_min = -10, val_max = 10):
|
||||||
@ -126,7 +143,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
form1 = "{a**2}x^2 + {2*a*b}x + {b**2}"
|
form1 = "{a**2}x^2 + {2*a*b}x + {b**2}"
|
||||||
cond1 = []
|
cond1 = []
|
||||||
rdExp1 = RdExpression(form1, cond1)
|
rdExp1 = RdExpression(form1, cond1, with_Exp = False)
|
||||||
desc_rdExp(rdExp1)
|
desc_rdExp(rdExp1)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user