random expression is now a Expression classmethod

This commit is contained in:
Lafrite 2014-12-19 16:45:22 +01:00
parent 5df17b4779
commit d817002430
2 changed files with 28 additions and 60 deletions

View File

@ -6,6 +6,8 @@ from .render import txt, tex
from .str2tokens import str2tokens from .str2tokens import str2tokens
from .operator import op from .operator import op
from .random_expression import RdExpression
__all__ = ['Expression'] __all__ = ['Expression']
class Expression(object): class Expression(object):
@ -22,6 +24,19 @@ class Expression(object):
def set_df_render(cls): def set_df_render(cls):
cls.set_render(cls.DEFAULT_RENDER) cls.set_render(cls.DEFAULT_RENDER)
@classmethod
def random(self, form="", conditions=[], val_min = -10, val_max=10):
"""Create a random expression from form and with conditions
: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 val_min: min value for generate variables
:param val_max: max value for generate variables
"""
random_generator = RdExpression(form, conditions)
return Expression(random_generator(val_min, val_max))
def __init__(self, exp): def __init__(self, exp):
""" Initiate the expression """ Initiate the expression
@ -284,6 +299,11 @@ if __name__ == '__main__':
#exp="-(-2)" #exp="-(-2)"
#test(exp) #test(exp)
print("\n")
exp = Expression.random("({a} + 3)({b} - 1)", ["{a} > 4"])
for i in exp.simplify():
print(i)
#import doctest #import doctest
#doctest.testmod() #doctest.testmod()

View File

@ -2,8 +2,6 @@
# encoding: utf-8 # encoding: utf-8
from random import randint from random import randint
from .expression import Expression
from .render import tex, txt
import re import re
import pyparsing import pyparsing
from .generic import flatten_list from .generic import flatten_list
@ -12,44 +10,11 @@ from .arithmetic import gcd
class RdExpression(object): class RdExpression(object):
""" """
A generator of random expression builder A generator of random expression builder
Two forms are available:
- exp: return the expression through Expression class (default)
- raw: return the expression as a raw string
This can be set globally with
RdExpression.FORM
or temporary with
- "raw_exp" or "raw_str" methods
""" """
FORM = "exp"
DEFAULT_FORM = "exp"
@classmethod
def set_form(cls, form):
""" Define whether RdExpression create expression with Expression (nice render) or if it only replace inside {} not taking care or render
>>> form = "{a}*{b}"
>>> exp = RdExpression(form)()
>>> print(type(exp))
<class 'pymath.expression.Expression'>
>>> RdExpression.set_form("raw")
>>> form = "{a}*{b}"
>>> exp = RdExpression(form)()
>>> print(type(exp))
<class 'str'>
"""
cls.FORM = form
@classmethod
def set_df_form(cls):
cls.set_form(cls.DEFAULT_FORM)
def __init__(self, form, conditions = []): def __init__(self, form, conditions = []):
"""Initiate the generator """Initiate the generator
@ -106,16 +71,16 @@ class RdExpression(object):
: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
:param render: Render of the expression (returns an Expression by default)
:returns: an formated random expression :returns: an formated random expression
""" """
if self.FORM == "exp": #if self.FORM == "exp":
return self.raw_exp(val_min, val_max) # return self.raw_exp(val_min, val_max)
elif self.FORM == "raw": #elif self.FORM == "raw":
return self.raw_str(val_min, val_max) # return self.raw_str(val_min, val_max)
else: #else:
raise ValueError(self.FORM , " is an undefined form for self.FORM") # raise ValueError(self.FORM , " is an undefined form for self.FORM")
return self.raw_str(val_min, val_max)
def raw_str(self, val_min = -10, val_max = 10): def raw_str(self, val_min = -10, val_max = 10):
"""Return raw string (don't use Expression for rendering or parsing) """Return raw string (don't use Expression for rendering or parsing)
@ -134,18 +99,6 @@ class RdExpression(object):
return exp 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)
def gene_varia(self, val_min = -10, val_max = 10): def gene_varia(self, val_min = -10, val_max = 10):
"""Randomly generates variables/letters """Randomly generates variables/letters
@ -185,10 +138,6 @@ def desc_rdExp(rdExp):
if __name__ == '__main__': if __name__ == '__main__':
# Adapt render to consol display
#Expression.set_render(txt)
RdExpression.set_form("raw")
form = "{a}*-14 / (2*{b}) / -23 / 4" form = "{a}*-14 / (2*{b}) / -23 / 4"
cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [1]", "{b} not in [1]"] cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [1]", "{b} not in [1]"]
rdExp1 = RdExpression(form, cond) rdExp1 = RdExpression(form, cond)
@ -196,7 +145,6 @@ if __name__ == '__main__':
rdExp2 = RdExpression(form) rdExp2 = RdExpression(form)
desc_rdExp(rdExp2) desc_rdExp(rdExp2)
RdExpression.set_df_form()
form = "{a+a*10}*4 + {a} + 2*{b}" form = "{a+a*10}*4 + {a} + 2*{b}"
cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "abs({a}) not in [1]", "{b} not in [1]", "gcd({a},{b}) == 1"] cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "abs({a}) not in [1]", "{b} not in [1]", "gcd({a},{b}) == 1"]
rdExp3 = RdExpression(form, cond) rdExp3 = RdExpression(form, cond)