diff --git a/expression.py b/expression.py index c268135..563e1cd 100644 --- a/expression.py +++ b/expression.py @@ -3,7 +3,7 @@ from generic import Stack, flatten_list, expand_list from fraction import Fraction -from render import txt_render, post2in_fix, tex_render +from renders import txt_render, post2in_fix, tex_render from formal import FormalExp from formal import FormalExp diff --git a/random_expression.py b/random_expression.py index 2a81215..7007226 100644 --- a/random_expression.py +++ b/random_expression.py @@ -3,6 +3,7 @@ from random import randint from expression import Expression +from renders import tex_render, txt_render import re class RdExpression(object): @@ -49,23 +50,13 @@ class RdExpression(object): return varia - def __call__(self, val_min = -10, val_max = 10): + def __call__(self, val_min = -10, val_max = 10, render = tex_render): """RdExpression once it is initiate act like a function which create random expressions. :param val_min: minimum value random generation :param val_max: maximum value random generation - :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 + :param render: Render of the expression (returns an Expression by default) + :returns: an formated random expression """ return render(self.raw_exp(val_min, val_max).postfix_tokens) @@ -108,13 +99,13 @@ class RdExpression(object): return True def desc_rdExp(rdExp): - from render import tex_render + from renders import tex_render print("--------------------") print("form: ",rdExp._form) print("Conditions: ",rdExp._conditions) print("Letters: ", rdExp._letters) print("2replaced: ", rdExp._2replaced) - print("Call : ", rdExp.render(render = tex_render)) + print("Call : ", rdExp(render = tex_render)) print("Gene varia: ", rdExp._gene_varia) print("Gene 2replaced: ", rdExp._gene_2replaced) print('') diff --git a/render.py b/render.py index a93112f..c3c872b 100644 --- a/render.py +++ b/render.py @@ -5,8 +5,6 @@ from generic import Stack,flatten_list from fraction import Fraction from formal import FormalExp - - class Render(object): """A class which aims to create render functions from three dictionnaries: - op_infix: dict of caracters @@ -201,53 +199,6 @@ class flist(list): pass -# ------------------------ -# A console render - -txt_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":"} -txt_postfix = {} -txt_other = {"(": "(", ")": ")"} - -txt_render = Render(txt_infix, txt_postfix, txt_other) - -# ------------------------ -# A infix to postfix list convertor - -p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":":":"} -p2i_postfix = {} -p2i_other = {"(": "(", ")": ")"} - -post2in_fix = Render(p2i_infix, p2i_postfix, p2i_other, join = False) - -# ------------------------ -# A latex render - -def texSlash(op1, op2): - if not Render.isNumerande(op1) and op1[0] == "(" and op1[-1] == ")": - op1 = op1[1:-1] - if not Render.isNumerande(op2) and op2[0] == "(" and op2[-1] == ")": - op2 = op2[1:-1] - return ["\\frac{" , op1 , "}{" , op2 , "}"] - -def texFrac(frac): - return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"] - -tex_infix = {"+": " + ", "-": " - ", "*": " \\times ", ":": ":"} -tex_postfix = {"/": texSlash} -tex_other = {"(": "(", ")": ")"} -tex_type_render = {int: str, Fraction: texFrac, FormalExp: str} - -tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_render) - - - -if __name__ == '__main__': - exp = [2, 5, '+', 1, '-', 3, 4, '*', ':'] - print(txt_render(exp)) - exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':'] - print(tex_render(exp)) - exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, '+'] - print(post2in_fix(exp)) # ----------------------------- # Reglages pour 'vim' diff --git a/renders.py b/renders.py new file mode 100644 index 0000000..51db589 --- /dev/null +++ b/renders.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# encoding: utf-8 + +from render import Render +from fraction import Fraction +from formal import FormalExp + +# ------------------------ +# A console render + +txt_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":"} +txt_postfix = {} +txt_other = {"(": "(", ")": ")"} + +txt_render = Render(txt_infix, txt_postfix, txt_other) + +# ------------------------ +# A infix to postfix list convertor + +p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":":":"} +p2i_postfix = {} +p2i_other = {"(": "(", ")": ")"} + +post2in_fix = Render(p2i_infix, p2i_postfix, p2i_other, join = False) + +# ------------------------ +# A latex render + +def texSlash(op1, op2): + if not Render.isNumerande(op1) and op1[0] == "(" and op1[-1] == ")": + op1 = op1[1:-1] + if not Render.isNumerande(op2) and op2[0] == "(" and op2[-1] == ")": + op2 = op2[1:-1] + return ["\\frac{" , op1 , "}{" , op2 , "}"] + +def texFrac(frac): + return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"] + +tex_infix = {"+": " + ", "-": " - ", "*": " \\times ", ":": ":"} +tex_postfix = {"/": texSlash} +tex_other = {"(": "(", ")": ")"} +tex_type_render = {int: str, Fraction: texFrac, FormalExp: str} + +tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_render) + + + +if __name__ == '__main__': + exp = [2, 5, '+', 1, '-', 3, 4, '*', ':'] + print(txt_render(exp)) + exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':'] + print(tex_render(exp)) + exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, '+'] + print(post2in_fix(exp)) + + + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del