From e24bff23dbbc81723fbee718a527c0b36e6ab584 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 13 Oct 2019 22:03:29 +0200 Subject: [PATCH] Feat: tokens can operate with builtin int --- mapytex/calculus/API/tokens/token.py | 80 +++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/mapytex/calculus/API/tokens/token.py b/mapytex/calculus/API/tokens/token.py index 3d3d3fa..e5d9ea7 100644 --- a/mapytex/calculus/API/tokens/token.py +++ b/mapytex/calculus/API/tokens/token.py @@ -11,6 +11,7 @@ Tokens: practical envelop of math object """ from ..renders import renders +from ...core.MO.atoms import moify class Token(object): @@ -26,7 +27,11 @@ class Token(object): self._ancestor = ancestor @classmethod - def random(cls): + def random( + cls, + family="integer", + **kwds + ): raise NotImplemented @classmethod @@ -86,7 +91,10 @@ class Token(object): from . import factory if not isinstance(other, Token): - _other = factory(other) + try: + _other = factory(other) + except AttributeError: + _other = factory(Expression(moify(other))) else: _other = other tree = Tree(operation, self._mo, _other._mo) @@ -106,6 +114,14 @@ class Token(object): ... print(i) 3 + 7 10 + >>> a = Integer(3) + >>> c = a + 7 + >>> c + + >>> for i in c.explain(): + ... print(i) + 3 + 7 + 10 >>> from .number import Fraction >>> a = Fraction("4/3") >>> b = Integer(7) @@ -137,6 +153,9 @@ class Token(object): ... print(i) 3 * 7 21 + >>> c = a * 7 + >>> c + >>> from .number import Fraction >>> a = Fraction("4/3") >>> b = Integer(7) @@ -164,6 +183,9 @@ class Token(object): >>> for i in c.explain(): ... print(i) 3 / 7 + >>> c = a / 7 + >>> c + >>> from .number import Fraction >>> a = Fraction("4/3") >>> b = Integer(7) @@ -179,6 +201,60 @@ class Token(object): """ 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 + 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 + + """ + return self._roperate(other, "+") + + 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 + + """ + 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 + + """ + return self._roperate(other, "/") + + def _get_soul(self, other=None): """ Get the builtin soul of self or other """ if isinstance(other, Token):