Rearrange folders and use tree2txt inside MO

This commit is contained in:
Bertrand Benjamin 2018-03-10 08:44:01 +03:00
parent d498af2bab
commit d14850c78d
8 changed files with 54 additions and 46 deletions

View File

@ -7,7 +7,7 @@
# Distributed under terms of the MIT license. # Distributed under terms of the MIT license.
""" """
Abstracts tools for calculs manipulations Make calculus as a student
""" """
__all__ = [] __all__ = []

View File

@ -32,30 +32,20 @@ class MOFraction(MO):
>>> f >>> f
<MOFraction - 2 / 3> <MOFraction - 2 / 3>
""" """
value = Tree("/", base_value = Tree("/",
MO.factory(numerator), MO.factory(numerator),
MO.factory(denominator), MO.factory(denominator),
) )
if negative:
value = Tree("-", None, base_value)
else:
value = base_value
MO.__init__(self, value) MO.__init__(self, value)
self._numerator = numerator self._numerator = numerator
self._denominator = denominator self._denominator = denominator
self.negative = negative 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): def __add__(self, other):
""" Overload * for MOFraction """ Overload * for MOFraction

View File

@ -6,9 +6,11 @@
# #
# Distributed under terms of the MIT license. # 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 from decimal import Decimal
__all__ = ["moify", "MO", "MOstr"] __all__ = ["moify", "MO", "MOstr"]
class MOError(Exception): class MOError(Exception):
@ -84,6 +86,9 @@ class MO(object):
@property @property
def __txt__(self): def __txt__(self):
try:
return tree2txt(self.value)
except AttributeError:
return str(self.value) return str(self.value)
def __add__(self, other): def __add__(self, other):
@ -144,6 +149,7 @@ class MO(object):
""" """
return MO.factory(-self.value) return MO.factory(-self.value)
class MOnumber(MO): class MOnumber(MO):
""" Base number math object (int or Decimal) """ """ Base number math object (int or Decimal) """
@ -194,15 +200,12 @@ class MOstr(MO):
""" Unknown math object like x or n""" """ Unknown math object like x or n"""
def __init__(self, value, negative=False): def __init__(self, value):
""" Initiate a string MO """ Initiate a string MO
>>> a = MOstr("x") >>> a = MOstr("x")
>>> a >>> a
<MOstr x> <MOstr x>
>>> a = MOstr("x", negative=True)
>>> a
<MOstr -x>
>>> b = MOstr(a) >>> b = MOstr(a)
>>> b >>> b
<MOstr x> <MOstr x>
@ -234,13 +237,8 @@ class MOstr(MO):
raise MOError(f"An MOstr should be initiate with a alpha string, got {val}") raise MOError(f"An MOstr should be initiate with a alpha string, got {val}")
MO.__init__(self, value) MO.__init__(self, value)
self.negative = negative
self.is_scalar = False self.is_scalar = False
@property
def __txt__(self):
return "-"*self.negative + str(self.value)
def __add__(self, other): def __add__(self, other):
raise NotImplemented raise NotImplemented
@ -268,7 +266,7 @@ class MOstr(MO):
raise NotImplemented raise NotImplemented
def __neg__(self): def __neg__(self):
return MO(self.value, not self.negative) pass
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

@ -10,10 +10,6 @@
Abstracts tools for calculs manipulations Abstracts tools for calculs manipulations
""" """
__all__ = ["Tree"]
from .tree import Tree
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

@ -22,8 +22,8 @@ def coroutine(func):
return cr return cr
return start return start
class STOOOP(BaseException): pass class STOOOP(Exception): pass
class RESTAAART(BaseException): pass class RESTAAART(Exception): pass

View File

@ -6,6 +6,8 @@
# #
# Distributed under terms of the MIT license. # Distributed under terms of the MIT license.
__all__ = ["OperatorError", "OPERATORS", "is_operator"]
class OperatorError(Exception): class OperatorError(Exception):
pass pass

View File

@ -0,0 +1,20 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# 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

View File

@ -6,15 +6,17 @@
# #
# Distributed under terms of the MIT license. # Distributed under terms of the MIT license.
from .operator import OPERATORS from mapytex.calculus.core.operator import OPERATORS
__all__ = ['tree2txt']
def plus2txt(left, right): def plus2txt(left, right):
""" + rendering """ + rendering
>>> from .MO import mo >>> from ..MO import mo
>>> plus2txt(mo.MOnumber(2), mo.MOnumber(3)) >>> plus2txt(mo.MOnumber(2), mo.MOnumber(3))
'2 + 3' '2 + 3'
>>> from .tree import Tree >>> from ..tree import Tree
>>> t = Tree.from_str("1+2") >>> t = Tree.from_str("1+2")
>>> plus2txt(t, mo.MOnumber(3)) >>> plus2txt(t, mo.MOnumber(3))
'1 + 2 + 3' '1 + 2 + 3'
@ -52,10 +54,10 @@ def plus2txt(left, right):
def minus2txt(left, right): def minus2txt(left, right):
""" - rendering """ - rendering
>>> from .MO import mo >>> from ..MO import mo
>>> minus2txt(None, mo.MO(3)) >>> minus2txt(None, mo.MO(3))
'- 3' '- 3'
>>> from .tree import Tree >>> from ..tree import Tree
>>> t = Tree.from_str("1+2") >>> t = Tree.from_str("1+2")
>>> minus2txt(None, t) >>> minus2txt(None, t)
'- (1 + 2)' '- (1 + 2)'
@ -77,10 +79,10 @@ def minus2txt(left, right):
def mul2txt(left, right): def mul2txt(left, right):
""" * rendering """ * rendering
>>> from .MO import mo >>> from ..MO import mo
>>> mul2txt(mo.MO(2), mo.MO(3)) >>> mul2txt(mo.MO(2), mo.MO(3))
'2 * 3' '2 * 3'
>>> from .tree import Tree >>> from ..tree import Tree
>>> t = Tree.from_str("1*2") >>> t = Tree.from_str("1*2")
>>> mul2txt(t, mo.MO(3)) >>> mul2txt(t, mo.MO(3))
'1 * 2 * 3' '1 * 2 * 3'
@ -130,10 +132,10 @@ def mul2txt(left, right):
def div2txt(left, right): def div2txt(left, right):
""" / rendering """ / rendering
>>> from .MO import mo >>> from ..MO import mo
>>> div2txt(mo.MO(2), mo.MO(3)) >>> div2txt(mo.MO(2), mo.MO(3))
'2 / 3' '2 / 3'
>>> from .tree import Tree >>> from ..tree import Tree
>>> t = Tree.from_str("1/2") >>> t = Tree.from_str("1/2")
>>> div2txt(t, mo.MO(3)) >>> div2txt(t, mo.MO(3))
'1 / 2 / 3' '1 / 2 / 3'
@ -172,10 +174,10 @@ def div2txt(left, right):
def pow2txt(left, right): def pow2txt(left, right):
""" ^ rendering """ ^ rendering
>>> from .MO import mo >>> from ..MO import mo
>>> pow2txt(mo.MO(2), mo.MO(3)) >>> pow2txt(mo.MO(2), mo.MO(3))
'2 ^ 3' '2 ^ 3'
>>> from .tree import Tree >>> from ..tree import Tree
>>> t = Tree.from_str("1^2") >>> t = Tree.from_str("1^2")
>>> pow2txt(t, mo.MO(3)) >>> pow2txt(t, mo.MO(3))
'1 ^ 2 ^ 3' '1 ^ 2 ^ 3'
@ -224,7 +226,7 @@ def tree2txt(tree):
It calls __txt__ to render MOs. It calls __txt__ to render MOs.
>>> from .tree import Tree >>> from ..tree import Tree
>>> t = Tree.from_str("2+3*4") >>> t = Tree.from_str("2+3*4")
>>> tree2txt(t) >>> tree2txt(t)
'2 + 3 * 4' '2 + 3 * 4'