From 7feca92d773b7d74f9dbd4ce004ce34e056c43fd Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Tue, 15 Mar 2016 12:43:34 +0300 Subject: [PATCH] move no_repetition in decorators all test are passed --- pymath/calculus/decorators.py | 58 +++++++++++++++++++++++++++++++++++ pymath/calculus/explicable.py | 25 +++------------ 2 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 pymath/calculus/decorators.py diff --git a/pymath/calculus/decorators.py b/pymath/calculus/decorators.py new file mode 100644 index 0000000..d533968 --- /dev/null +++ b/pymath/calculus/decorators.py @@ -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 diff --git a/pymath/calculus/explicable.py b/pymath/calculus/explicable.py index de209ef..0a54f1c 100644 --- a/pymath/calculus/explicable.py +++ b/pymath/calculus/explicable.py @@ -5,6 +5,7 @@ from .renderable import Renderable from .step import Step from decimal import Decimal from .generic import transpose_fill +from .decorators import is_same_step, no_repetition class Explicable(Renderable): @@ -56,6 +57,7 @@ class Explicable(Renderable): """ Generator which yield itself rendered """ yield self.STR_RENDER(self.postfix_tokens) + @no_repetition(is_same_step) def history_generator(self): r""" Generator for rendered steps which leed to itself @@ -79,39 +81,20 @@ class Explicable(Renderable): 2 + 40 42 """ - old_s = '' try: for s in self.steps: try: new_s = self.STR_RENDER(s.postfix_tokens) except AttributeError: 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: pass # if noself: new_s = self.STR_RENDER(self.postfix_tokens) - if not self.is_same_step(new_s, old_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 + yield new_s def this_append_before(self, steps): """ Add steps at the beginning of self.steps