From d14850c78d8e83eeee92436674af8c7dc1a36ba7 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sat, 10 Mar 2018 08:44:01 +0300 Subject: [PATCH] Rearrange folders and use tree2txt inside MO --- mapytex/calculus/__init__.py | 2 +- mapytex/calculus/core/MO/fraction.py | 20 ++++---------- mapytex/calculus/core/MO/mo.py | 22 +++++++--------- mapytex/calculus/core/__init__.py | 4 --- mapytex/calculus/core/coroutine.py | 4 +-- mapytex/calculus/core/operator.py | 2 ++ mapytex/calculus/core/renders/__init__.py | 20 ++++++++++++++ .../calculus/core/{ => renders}/tree2txt.py | 26 ++++++++++--------- 8 files changed, 54 insertions(+), 46 deletions(-) create mode 100644 mapytex/calculus/core/renders/__init__.py rename mapytex/calculus/core/{ => renders}/tree2txt.py (93%) diff --git a/mapytex/calculus/__init__.py b/mapytex/calculus/__init__.py index 6b4cdfd..d9308ef 100644 --- a/mapytex/calculus/__init__.py +++ b/mapytex/calculus/__init__.py @@ -7,7 +7,7 @@ # Distributed under terms of the MIT license. """ -Abstracts tools for calculs manipulations +Make calculus as a student """ __all__ = [] diff --git a/mapytex/calculus/core/MO/fraction.py b/mapytex/calculus/core/MO/fraction.py index 36e2512..178abd3 100644 --- a/mapytex/calculus/core/MO/fraction.py +++ b/mapytex/calculus/core/MO/fraction.py @@ -32,30 +32,20 @@ class MOFraction(MO): >>> f """ - value = Tree("/", + base_value = Tree("/", MO.factory(numerator), MO.factory(denominator), ) + if negative: + value = Tree("-", None, base_value) + else: + value = base_value MO.__init__(self, value) self._numerator = numerator self._denominator = denominator self.negative = negative - @property - def __txt__(self): - # TODO: fonctionnement temporaire. Il faudrait utilser un moteur de rendu plus fin. |jeu. mars 8 15:26:49 EAT 2018 - try: - numerator = self._numerator.__txt__ - except AttributeError: - numerator = str(self._numerator) - - try: - denominator = self._denominator.__txt__ - except AttributeError: - denominator = str(self._denominator) - return "- "*self.negative + f"{numerator} / {denominator}" - def __add__(self, other): """ Overload * for MOFraction diff --git a/mapytex/calculus/core/MO/mo.py b/mapytex/calculus/core/MO/mo.py index 043b25b..737d79d 100644 --- a/mapytex/calculus/core/MO/mo.py +++ b/mapytex/calculus/core/MO/mo.py @@ -6,9 +6,11 @@ # # Distributed under terms of the MIT license. -from mapytex.calculus.core.coroutine import coroutine, STOOOP +from ..coroutine import coroutine, STOOOP +from ..renders import tree2txt from decimal import Decimal + __all__ = ["moify", "MO", "MOstr"] class MOError(Exception): @@ -84,7 +86,10 @@ class MO(object): @property def __txt__(self): - return str(self.value) + try: + return tree2txt(self.value) + except AttributeError: + return str(self.value) def __add__(self, other): """ Overload + for MOs @@ -144,6 +149,7 @@ class MO(object): """ return MO.factory(-self.value) + class MOnumber(MO): """ Base number math object (int or Decimal) """ @@ -194,15 +200,12 @@ class MOstr(MO): """ Unknown math object like x or n""" - def __init__(self, value, negative=False): + def __init__(self, value): """ Initiate a string MO >>> a = MOstr("x") >>> a - >>> a = MOstr("x", negative=True) - >>> a - >>> b = MOstr(a) >>> b @@ -234,13 +237,8 @@ class MOstr(MO): raise MOError(f"An MOstr should be initiate with a alpha string, got {val}") MO.__init__(self, value) - self.negative = negative self.is_scalar = False - @property - def __txt__(self): - return "-"*self.negative + str(self.value) - def __add__(self, other): raise NotImplemented @@ -268,7 +266,7 @@ class MOstr(MO): raise NotImplemented def __neg__(self): - return MO(self.value, not self.negative) + pass # ----------------------------- # Reglages pour 'vim' diff --git a/mapytex/calculus/core/__init__.py b/mapytex/calculus/core/__init__.py index e999710..47ae35f 100644 --- a/mapytex/calculus/core/__init__.py +++ b/mapytex/calculus/core/__init__.py @@ -10,10 +10,6 @@ Abstracts tools for calculs manipulations """ -__all__ = ["Tree"] - -from .tree import Tree - # ----------------------------- # Reglages pour 'vim' diff --git a/mapytex/calculus/core/coroutine.py b/mapytex/calculus/core/coroutine.py index b2f00db..a34be60 100644 --- a/mapytex/calculus/core/coroutine.py +++ b/mapytex/calculus/core/coroutine.py @@ -22,8 +22,8 @@ def coroutine(func): return cr return start -class STOOOP(BaseException): pass -class RESTAAART(BaseException): pass +class STOOOP(Exception): pass +class RESTAAART(Exception): pass diff --git a/mapytex/calculus/core/operator.py b/mapytex/calculus/core/operator.py index 597699e..e794692 100644 --- a/mapytex/calculus/core/operator.py +++ b/mapytex/calculus/core/operator.py @@ -6,6 +6,8 @@ # # Distributed under terms of the MIT license. +__all__ = ["OperatorError", "OPERATORS", "is_operator"] + class OperatorError(Exception): pass diff --git a/mapytex/calculus/core/renders/__init__.py b/mapytex/calculus/core/renders/__init__.py new file mode 100644 index 0000000..cde6421 --- /dev/null +++ b/mapytex/calculus/core/renders/__init__.py @@ -0,0 +1,20 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 lafrite +# +# Distributed under terms of the MIT license. + +""" +Tree renders +""" + +__all__ = ["tree2txt"] + +from .tree2txt import tree2txt + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del diff --git a/mapytex/calculus/core/tree2txt.py b/mapytex/calculus/core/renders/tree2txt.py similarity index 93% rename from mapytex/calculus/core/tree2txt.py rename to mapytex/calculus/core/renders/tree2txt.py index 98c3cce..3cb384e 100644 --- a/mapytex/calculus/core/tree2txt.py +++ b/mapytex/calculus/core/renders/tree2txt.py @@ -6,15 +6,17 @@ # # Distributed under terms of the MIT license. -from .operator import OPERATORS +from mapytex.calculus.core.operator import OPERATORS + +__all__ = ['tree2txt'] def plus2txt(left, right): """ + rendering - >>> from .MO import mo + >>> from ..MO import mo >>> plus2txt(mo.MOnumber(2), mo.MOnumber(3)) '2 + 3' - >>> from .tree import Tree + >>> from ..tree import Tree >>> t = Tree.from_str("1+2") >>> plus2txt(t, mo.MOnumber(3)) '1 + 2 + 3' @@ -52,10 +54,10 @@ def plus2txt(left, right): def minus2txt(left, right): """ - rendering - >>> from .MO import mo + >>> from ..MO import mo >>> minus2txt(None, mo.MO(3)) '- 3' - >>> from .tree import Tree + >>> from ..tree import Tree >>> t = Tree.from_str("1+2") >>> minus2txt(None, t) '- (1 + 2)' @@ -77,10 +79,10 @@ def minus2txt(left, right): def mul2txt(left, right): """ * rendering - >>> from .MO import mo + >>> from ..MO import mo >>> mul2txt(mo.MO(2), mo.MO(3)) '2 * 3' - >>> from .tree import Tree + >>> from ..tree import Tree >>> t = Tree.from_str("1*2") >>> mul2txt(t, mo.MO(3)) '1 * 2 * 3' @@ -130,10 +132,10 @@ def mul2txt(left, right): def div2txt(left, right): """ / rendering - >>> from .MO import mo + >>> from ..MO import mo >>> div2txt(mo.MO(2), mo.MO(3)) '2 / 3' - >>> from .tree import Tree + >>> from ..tree import Tree >>> t = Tree.from_str("1/2") >>> div2txt(t, mo.MO(3)) '1 / 2 / 3' @@ -172,10 +174,10 @@ def div2txt(left, right): def pow2txt(left, right): """ ^ rendering - >>> from .MO import mo + >>> from ..MO import mo >>> pow2txt(mo.MO(2), mo.MO(3)) '2 ^ 3' - >>> from .tree import Tree + >>> from ..tree import Tree >>> t = Tree.from_str("1^2") >>> pow2txt(t, mo.MO(3)) '1 ^ 2 ^ 3' @@ -224,7 +226,7 @@ def tree2txt(tree): It calls __txt__ to render MOs. - >>> from .tree import Tree + >>> from ..tree import Tree >>> t = Tree.from_str("2+3*4") >>> tree2txt(t) '2 + 3 * 4'