Integrate exp and render into random_expression

This commit is contained in:
lafrite 2014-01-18 19:25:10 +01:00
parent 047fb903e7
commit bb12e67557
2 changed files with 39 additions and 10 deletions

View File

@ -127,11 +127,15 @@ class Expression(object):
if character.isdigit(): if character.isdigit():
# for "big" numbers (like 2345) # for "big" numbers (like 2345)
if type(tokens[-1]) == int: if type(tokens[-1]) == int:
if tokens[-1] > 0:
tokens[-1] = tokens[-1]*10 + int(character) tokens[-1] = tokens[-1]*10 + int(character)
else:
tokens[-1] = tokens[-1]*10 - int(character)
# Special case for "-" at the begining of an expression or before "(" # Special case for "-" at the begining of an expression or before "("
elif tokens[-1] == "-" and \ elif tokens[-1] == "-" and \
str(tokens[-2]) in " (": str(tokens[-2]) in " (+-*/":
tokens[-1] = - int(character) tokens[-1] = - int(character)
else: else:
tokens.append(int(character)) tokens.append(int(character))

View File

@ -2,6 +2,7 @@
# encoding: utf-8 # encoding: utf-8
from random import randint from random import randint
from expression import Expression
import re import re
class RdExpression(object): class RdExpression(object):
@ -53,7 +54,28 @@ 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
:returns: an random expression :returns: an random expression formated for console printing
"""
return str(self.raw_exp(val_min, val_max))
def render(self, val_min = -10, val_max = 10, render = lambda x: str(x)):
"""Same as __call_ but uses render from the Expression object
:param val_min: minimum value random generation
:param val_max: maximum value random generation
:param render: function which render the list of token (postfix form) to string
:returns: an random expression formated by render
"""
return render(self.raw_exp(val_min, val_max).postfix_tokens)
def raw_exp(self, val_min = -10, val_max = 10):
"""Same as __call_ but returns an Expression object
:param val_min: minimum value random generation
:param val_max: maximum value random generation
:returns: an random Expression object
""" """
self.gene_varia(val_min, val_max) self.gene_varia(val_min, val_max)
@ -61,7 +83,9 @@ class RdExpression(object):
while not(self.val_conditions()): while not(self.val_conditions()):
self.gene_varia(val_min, val_max) self.gene_varia(val_min, val_max)
return self._form.format(**self._gene_2replaced) exp = self._form.format(**self._gene_2replaced)
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
@ -84,28 +108,29 @@ class RdExpression(object):
return True return True
def desc_rdExp(rdExp): def desc_rdExp(rdExp):
from render import tex_render
print("--------------------") print("--------------------")
print("form: ",rdExp._form) print("form: ",rdExp._form)
print("Conditions: ",rdExp._conditions) print("Conditions: ",rdExp._conditions)
print("Letters: ", rdExp._letters) print("Letters: ", rdExp._letters)
print("2replaced: ", rdExp._2replaced) print("2replaced: ", rdExp._2replaced)
print("Call : ", rdExp()) print("Call : ", rdExp.render(render = tex_render))
print("Gene varia: ", rdExp._gene_varia) print("Gene varia: ", rdExp._gene_varia)
print("Gene 2replaced: ", rdExp._gene_2replaced) print("Gene 2replaced: ", rdExp._gene_2replaced)
print('') print('')
if __name__ == '__main__': if __name__ == '__main__':
form = "{a}x + 2*{b}" form = "{a}*-14 / 2*{b} + -23"
cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"] cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"]
rdExp1 = RdExpression(form, cond) rdExp1 = RdExpression(form, cond)
desc_rdExp(rdExp1) desc_rdExp(rdExp1)
rdExp2 = RdExpression(form) rdExp2 = RdExpression(form)
desc_rdExp(rdExp2) desc_rdExp(rdExp2)
form = "{a+a/10}x + {a} + 2*{b}" #form = "{a+a/10}x + {a} + 2*{b}"
cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"] #cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"]
rdExp3 = RdExpression(form) #rdExp3 = RdExpression(form)
desc_rdExp(rdExp3) #desc_rdExp(rdExp3)