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

93 lines
2.0 KiB
Python

#! /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
"""
from ..renders import renders
class Token(object):
""" Token: practical envelop of an math object """
RENDER = 'txt'
def __init__(self, mo, name="", ancestor = None):
self._mo = mo
self.name = name
self._mathtype = None
self._ancestor = ancestor
@classmethod
def random(cls):
raise NotImplemented
def explain(self):
""" Yield every calculus step which have lead to self
: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):
return f"<{self.__class__.__name__} {self.__txt__}>"
def __str__(self):
return renders[self.RENDER](self._mo)
@property
def __txt__(self):
return self._mo.__txt__
@property
def __tex__(self):
return self._mo.__tex__
def __add__(self, other):
""" Adding 2 Tokens or a Token and a Expression
:example:
>>> from .number import Integer
>>> a = Integer(3)
"""
from ..expression import Expression
from ...core import Tree
from . import factory
if not isinstance(other, Token):
_other = factory(other)
else:
_other = other
tree = Tree('+', self.mo, _other.mo)
ans = Expression(tree).simplify()
return ans
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del