2016-03-15 09:43:34 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
from functools import wraps
|
|
|
|
|
2016-03-26 02:37:51 +00:00
|
|
|
def no_repetition(equals=lambda x,y: x == y, old = ""):
|
2016-03-15 09:43:34 +00:00
|
|
|
""" Remove yield values which has already been yield
|
|
|
|
|
2016-03-26 02:37:51 +00:00
|
|
|
:param equals: test for equality
|
|
|
|
:param old: initiate form of data.
|
2016-03-15 09:43:34 +00:00
|
|
|
: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)
|
2016-03-26 02:37:51 +00:00
|
|
|
old_s = old
|
2016-03-15 09:43:34 +00:00
|
|
|
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
|