def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
# https://docs.python.org/3/library/itertools.html#recipes
seen = set()
seen_add = seen.add
if key is None:
for element in itertools.ifilterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element
#
# Parsing function parameter strings (to find the parameters without default values).
#
python类html()的实例源码
def __iter__(self):
import itertools
# roundrobin recipe taken from itertools documentation:
# https://docs.python.org/2/library/itertools.html#recipes
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
if PY3:
nexts = itertools.cycle(iter(it).__next__ for it in iterables)
else:
nexts = itertools.cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = itertools.cycle(itertools.islice(nexts, pending))
if all(set.is_iterable for set in self.args):
return roundrobin(*(iter(arg) for arg in self.args))
else:
raise TypeError("Not all constituent sets are iterable")
def grouper(n, iterable, fillvalue=None):
# http://docs.python.org/dev/3.0/library/itertools.html#module-itertools
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.zip_longest(fillvalue=fillvalue, *args)
def __copy__(self):
"""
Clone the iterator include states
"""
# https://docs.python.org/2/library/itertools.html#itertools.tee
# tee is not that helpful here, and it will also occupy a lot of memory
# self._file_handler, new_iter = itertools.tee(self._file_handler)
new_iter = FileIterator(self._file_path, self._type, **self._kwargs)
if self._line_count > 0:
for _ in new_iter:
if new_iter._line_count == self._line_count:
break
return new_iter
def powerset(iterable, maxSets=None):
"""
powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
See https://docs.python.org/2/library/itertools.html#recipes
"""
s = list(iterable)
return itertools.islice(itertools.chain.from_iterable(
itertools.combinations(s, r) for r in range(len(s) + 1)),
0, maxSets)
def lasts(iterable, items=1, default=None):
# type: (Iterable[T], int, T) -> Iterable[T]
""" Lazily return the last x items from this iterable or default. """
last_items = deque(iterable, maxlen=items)
for _ in range(items - len(last_items)):
yield default
for y in last_items:
yield y
# reduce is technically the last value of accumulate
# use ww.utils.EMPTY instead of EMPTY
# Put in the doc than scan=fold=accumulare and reduce=accumulate
# replace https://docs.python.org/3/library/itertools.html#itertools.accumulate
# that works only on Python 3.3 and doesn't have echo_start
# def accumulate(func, iterable, start=ww.utils.EMPTY, *, echo_start=True):
# """
# Scan higher-order function.
# The first 3 positional arguments are alike to the ``functools.reduce``
# signature. This function accepts an extra optional ``echo_start``
# parameter that controls whether the first value should be in the output.
# """
# it = iter(iterable)
# if start is ww.utils._EMPTY:
# start = next(it)
# if echo_start:
# yield start
# for item in it:
# start = func(start, item)
# yield start
def progress(prompt, percent, bar_length=20):
hashes = '#' * int(round(percent * bar_length))
spaces = ' ' * (bar_length - len(hashes))
return "\r{}: [{}] {}%".format(
prompt,
hashes + spaces,
int(round(percent * 100)))
#ref: https://docs.python.org/3.5/library/itertools.html#itertools.zip_longest
def grouper(iterable, n, fillvalue=None):
"""Collect data into fixed-length chunks or blocks.
Source: https://docs.python.org/2/library/itertools.html#recipes
"""
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
def product(*args, **kwds):
"""
Taken from http://docs.python.org/library/itertools.html#itertools.product
"""
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
def grouper(iterable, n, fillvalue=None):
"""
Collect data into fixed-length chunks or blocks
Recipe from itertools documentation
https://docs.python.org/2/library/itertools.html
"""
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return itertools.zip_longest(fillvalue=fillvalue, *args)