move no_repetition in decorators
all test are passed
This commit is contained in:
parent
f74f41c131
commit
7feca92d77
58
pymath/calculus/decorators.py
Normal file
58
pymath/calculus/decorators.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
def is_same_step(new, old):
|
||||||
|
"""Return whether the new step is the same than old step
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if new.replace(" ", "") == old.replace(" ", ""):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
except AttributeError:
|
||||||
|
if new == old:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def no_repetition(equals=lambda x,y: x == y):
|
||||||
|
""" Remove yield values which has already been yield
|
||||||
|
|
||||||
|
:param fun: a generator
|
||||||
|
:returns: same generator with no repetitions
|
||||||
|
|
||||||
|
>>> norep = no_repetition()
|
||||||
|
>>> def str_gene(string):
|
||||||
|
... for i in string:
|
||||||
|
... yield i
|
||||||
|
>>> norep_str_gene = norep(str_gene)
|
||||||
|
>>> [i for i in norep_str_gene("aaabraabbbere")]
|
||||||
|
['a', 'b', 'r', 'a', 'b', 'e', 'r', 'e']
|
||||||
|
|
||||||
|
"""
|
||||||
|
def wrapper(fun):
|
||||||
|
@wraps(fun)
|
||||||
|
def dont_repeat(*args, **kwrds):
|
||||||
|
gen = fun(*args, **kwrds)
|
||||||
|
old_s = ""
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
new_s = next(gen)
|
||||||
|
except StopIteration:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
if not equals(new_s, old_s):
|
||||||
|
old_s = new_s
|
||||||
|
yield new_s
|
||||||
|
|
||||||
|
return dont_repeat
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
|
# Reglages pour 'vim'
|
||||||
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||||
|
# cursor: 16 del
|
@ -5,6 +5,7 @@ from .renderable import Renderable
|
|||||||
from .step import Step
|
from .step import Step
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from .generic import transpose_fill
|
from .generic import transpose_fill
|
||||||
|
from .decorators import is_same_step, no_repetition
|
||||||
|
|
||||||
|
|
||||||
class Explicable(Renderable):
|
class Explicable(Renderable):
|
||||||
@ -56,6 +57,7 @@ class Explicable(Renderable):
|
|||||||
""" Generator which yield itself rendered """
|
""" Generator which yield itself rendered """
|
||||||
yield self.STR_RENDER(self.postfix_tokens)
|
yield self.STR_RENDER(self.postfix_tokens)
|
||||||
|
|
||||||
|
@no_repetition(is_same_step)
|
||||||
def history_generator(self):
|
def history_generator(self):
|
||||||
r""" Generator for rendered steps which leed to itself
|
r""" Generator for rendered steps which leed to itself
|
||||||
|
|
||||||
@ -79,16 +81,12 @@ class Explicable(Renderable):
|
|||||||
2 + 40
|
2 + 40
|
||||||
42
|
42
|
||||||
"""
|
"""
|
||||||
old_s = ''
|
|
||||||
try:
|
try:
|
||||||
for s in self.steps:
|
for s in self.steps:
|
||||||
try:
|
try:
|
||||||
new_s = self.STR_RENDER(s.postfix_tokens)
|
new_s = self.STR_RENDER(s.postfix_tokens)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
new_s = self.STR_RENDER(s)
|
new_s = self.STR_RENDER(s)
|
||||||
|
|
||||||
if not self.is_same_step(new_s, old_s):
|
|
||||||
old_s = new_s
|
|
||||||
yield new_s
|
yield new_s
|
||||||
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -96,23 +94,8 @@ class Explicable(Renderable):
|
|||||||
|
|
||||||
# if noself:
|
# if noself:
|
||||||
new_s = self.STR_RENDER(self.postfix_tokens)
|
new_s = self.STR_RENDER(self.postfix_tokens)
|
||||||
if not self.is_same_step(new_s, old_s):
|
|
||||||
yield new_s
|
yield new_s
|
||||||
|
|
||||||
def is_same_step(self, new, old):
|
|
||||||
"""Return whether the new step is the same than old step
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
if new.replace(" ", "") == old.replace(" ", ""):
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
except AttributeError:
|
|
||||||
if new == old:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def this_append_before(self, steps):
|
def this_append_before(self, steps):
|
||||||
""" Add steps at the beginning of self.steps
|
""" Add steps at the beginning of self.steps
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user