Mapytex/pymath/calculus/step.py
2016-03-22 08:39:20 +03:00

85 lines
2.3 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
from .renderable import Renderable
__all__ = ['Step']
class Step(Renderable):
"""
A step is a Renderable which his only goal is to be render.
"""
@classmethod
def tmp_render(cls):
""" Create a container in which the method explain return only Step object.
>>> from .expression import Expression
>>> exp = Expression("2*3/5")
>>> print(exp)
2 \\times \\frac{ 3 }{ 5 }
>>> for i in exp.simplify().explain():
... print(i)
2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 }
>>> with Step.tmp_render():
... for i in exp.simplify().explain():
... print(repr(i))
< Step [2, 3, 5, /, *]>
< Step [3, 5, /, 2, *]>
< Step [3, 2, *, 5, /]>
< Step [6, 5, /]>
>>> for i in exp.simplify().explain():
... print(i)
2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 }
"""
return super(Step, cls).tmp_render(Step)
def __init__(self, exp):
"""Initiate the renderable objet
:param pstf_tokens: the postfix list of tokens
>>> s = Step([2, 3, '+'])
>>> s
< Step [2, 3, '+']>
>>> s1 = Step([s, 5, '*'])
>>> s1
< Step [2, 3, '+', 5, '*']>
"""
if isinstance(exp, Renderable):
self.postfix_tokens = exp.postfix_tokens
elif isinstance(exp, list):
self.postfix_tokens = []
for t in exp:
try:
self.postfix_tokens += t.postfix_tokens
except AttributeError:
self.postfix_tokens.append(t)
else:
raise ValueError(
"Can't initiate Step with {}".format(
exp
))
def __eq__(self, other):
try:
if self.postfix_tokens == other.postfix_tokens:
return True
except AttributeError:
return False
return False
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del