Mapytex/mapytex/calculus/API/tokens/token.py

354 lines
8.5 KiB
Python
Raw Permalink Normal View History

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
"""
Tokens: practical envelop of math object
"""
2020-12-15 14:37:27 +00:00
from ..renders import render
from ...core.MO.atoms import moify
2019-05-14 04:55:56 +00:00
class Token(object):
""" Token: practical envelop of an math object """
2019-05-14 04:55:56 +00:00
def __init__(self, mo, name="", ancestor=None):
self._mo = mo
self.name = name
self._mathtype = None
self._ancestor = ancestor
@classmethod
def random(
cls,
family="integer",
**kwds
):
raise NotImplemented
def explain(self):
""" Yield every calculus step which have lead to self
2020-12-12 21:03:32 +00:00
:example:
>>> from mapytex.calculus.API import Expression
>>> e = Expression.from_str("2+3*4")
>>> f = e.simplify()
>>> f
<Integer 14>
>>> for s in f.explain():
... print(s)
2 + 3 * 4
2 + 12
14
"""
try:
yield from self._ancestor.explain()
yield self
except AttributeError:
yield self
def __repr__(self):
2021-09-25 16:05:05 +00:00
try:
return f"<{self.__class__.__name__} {render(self._mo._tree, 'txt')}>"
except AttributeError:
return f"<{self.__class__.__name__} {render(self._mo, 'txt')}>"
def __str__(self):
2021-09-25 16:05:05 +00:00
try:
return render(self._mo._tree)
except AttributeError:
return render(self._mo)
2019-10-16 20:45:41 +00:00
@property
def raw(self):
""" Get python's raw forme of the token """
2019-10-30 19:58:46 +00:00
return self._mo.content
2019-10-16 20:45:41 +00:00
2018-12-13 12:45:39 +00:00
def _operate(self, other, operation):
""" Make a operation between 2 Tokens """
from ..expression import Expression
from ...core import Tree
from . import factory
2019-05-14 04:55:56 +00:00
if not isinstance(other, Token):
try:
_other = factory(other)
except AttributeError:
_other = factory(Expression(moify(other)))
else:
_other = other
if operation == '-':
tree = Tree("+", self._mo, Tree("-", None, _other._mo))
else:
tree = Tree(operation, self._mo, _other._mo)
2018-12-13 12:45:39 +00:00
return Expression(tree).simplify()
def __add__(self, other):
""" Adding 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> b = Integer(7)
>>> c = a + b
>>> c
<Integer 10>
>>> for i in c.explain():
... print(i)
3 + 7
10
>>> a = Integer(3)
>>> c = a + 7
>>> c
<Integer 10>
>>> for i in c.explain():
... print(i)
3 + 7
10
>>> a = Integer(3)
>>> c = a + "x"
>>> c
<Linear x + 3>
2018-12-13 12:45:39 +00:00
>>> from .number import Fraction
>>> a = Fraction("4/3")
>>> b = Integer(7)
>>> c = a + b
>>> c
<Fraction 25 / 3>
>>> for i in c.explain():
... print(i)
4 / 3 + 7
4 / 3 + 7 / 1
4 / 3 + (7 * 3) / (1 * 3)
4 / 3 + 21 / 3
(4 + 21) / 3
25 / 3
"""
return self._operate(other, "+")
def __sub__(self, other):
""" Subing 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> b = Integer(7)
>>> c = a - b
>>> c
<Integer - 4>
>>> for i in c.explain():
... print(i)
3 - 7
3 - 7
- 4
>>> a = Integer(3)
>>> c = a - 7
>>> c
<Integer - 4>
>>> a = Integer(3)
>>> c = a - "x"
>>> c
<Linear - x + 3>
"""
return self._operate(other, "-")
2018-12-13 12:45:39 +00:00
def __mul__(self, other):
""" Multiply 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> b = Integer(7)
>>> c = a * b
>>> c
<Integer 21>
>>> for i in c.explain():
... print(i)
3 * 7
21
>>> c = a * 7
>>> c
<Integer 21>
>>> c = a * "x"
>>> c
<Linear 3x>
2018-12-13 12:45:39 +00:00
>>> from .number import Fraction
>>> a = Fraction("4/3")
>>> b = Integer(7)
>>> c = a * b
>>> c
<Fraction 28 / 3>
>>> for i in c.explain():
... print(i)
4 / 3 * 7
(4 * 7) / 3
28 / 3
"""
return self._operate(other, "*")
def __truediv__(self, other):
""" Divising 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> b = Integer(7)
>>> c = a / b
>>> c
<Fraction 3 / 7>
>>> for i in c.explain():
... print(i)
3 / 7
>>> c = a / 7
>>> c
<Fraction 3 / 7>
2018-12-13 12:45:39 +00:00
>>> from .number import Fraction
>>> a = Fraction("4/3")
>>> b = Integer(7)
>>> c = a / b
>>> c
<Fraction 4 / 21>
>>> for i in c.explain():
... print(i)
4 / 3 / 7
4 / 3 * 1 / 7
(4 * 1) / (3 * 7)
4 / 21
"""
return self._operate(other, "/")
2019-10-15 17:32:19 +00:00
def __pow__(self, other):
""" Token powered by an other
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> b = Integer(7)
>>> c = a ** b
>>> c
<Integer 2187>
>>> c = a ** 7
>>> c
<Integer 2187>
>>> from .number import Decimal
>>> a = Decimal('2.3')
>>> c = a ** 2
>>> c
<Decimal 5.29>
"""
2019-10-15 17:32:19 +00:00
return self._operate(other, "^")
def _roperate(self, other, operation):
""" Make a operation between 2 Tokens """
from ..expression import Expression
from ...core import Tree
from . import factory
if not isinstance(other, Token):
try:
_other = factory(other)
except AttributeError:
_other = factory(Expression(moify(other)))
else:
_other = other
if operation == '-':
tree = Tree("+", _other._mo, Tree("-", None, self._mo))
else:
tree = Tree(operation, _other._mo, self._mo)
return Expression(tree).simplify()
def __radd__(self, other):
""" Adding 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> c = 7 + a
>>> c
<Integer 10>
>>> c = "x" + a
>>> c
<Linear x + 3>
"""
return self._roperate(other, "+")
def __rsub__(self, other):
""" Subing 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> c = 7 - a
>>> c
<Integer 4>
>>> a = Integer(3)
>>> c = "x" - a
>>> c
<Linear x - 3>
"""
return self._roperate(other, "-")
2020-12-12 21:03:32 +00:00
def __rmul__(self, other):
""" Multiply 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> c = 7 * a
>>> c
<Integer 21>
>>> c = "x" * a
>>> c
<Linear 3x>
"""
return self._roperate(other, "*")
def __rtruediv__(self, other):
""" Divising 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
>>> c = 7 / a
>>> c
<Fraction 7 / 3>
"""
return self._roperate(other, "/")
def _get_soul(self, other=None):
""" Get the builtin soul of self or other """
if isinstance(other, Token):
return other._mo._value
elif not other is None:
return other
return self._mo._value
def __eq__(self, other):
return self._get_soul() == self._get_soul(other)
def __gt__(self, other):
return self._get_soul() > self._get_soul(other)
def __lt__(self, other):
return self._get_soul() < self._get_soul(other)
def __ge__(self, other):
return self._get_soul() >= self._get_soul(other)
def __le__(self, other):
return self._get_soul() <= self._get_soul(other)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del