2016-03-10 11:42:27 +00:00
|
|
|
#!/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():
|
2016-03-12 01:51:38 +00:00
|
|
|
... print(repr(i))
|
2016-03-10 11:42:27 +00:00
|
|
|
< 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)
|
|
|
|
|
2016-03-10 13:40:52 +00:00
|
|
|
def __init__(self, exp):
|
2016-03-10 11:42:27 +00:00
|
|
|
"""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, '*']>
|
|
|
|
|
|
|
|
"""
|
2016-03-10 13:40:52 +00:00
|
|
|
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
|
|
|
|
))
|
2016-03-10 11:42:27 +00:00
|
|
|
|
2016-03-19 12:30:52 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
try:
|
|
|
|
if self.postfix_tokens == other.postfix_tokens:
|
|
|
|
return True
|
|
|
|
except AttributeError:
|
2016-03-26 02:38:21 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
if str(self).replace(' ', '') == str(other).replace(' ',''):
|
|
|
|
return True
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2016-03-19 12:30:52 +00:00
|
|
|
|
|
|
|
return False
|
2016-03-10 11:42:27 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|