Mapytex/pymath/calculus/decorators.py
2016-03-26 05:37:51 +03:00

46 lines
1.2 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
from functools import wraps
def no_repetition(equals=lambda x,y: x == y, old = ""):
""" Remove yield values which has already been yield
:param equals: test for equality
:param old: initiate form of data.
: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 = old
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