Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
>>> import time
>>> start_time = time.time()
>>> flu(range(3)).rate_limit(3).collect()
>>> print('Runtime', time.time() - start_time)
1.00126 # approximately 1 second for 3 items
"""
def _impl():
wait_time = 1.0 / per_second
for val in self:
start_time = time.time()
yield val
call_duration = time.time() - start_time
time.sleep(max(wait_time - call_duration, 0.0))
return Fluent(_impl())
def map(self, func: Callable, *args, **kwargs):
"""Apply *func* to each element of iterable
>>> flu(range(5)).map(lambda x: x*x).collect()
[0, 1, 4, 9, 16]
"""
def _impl():
for val in self._iterable:
yield func(val, *args, **kwargs)
return Fluent(_impl())
def sort(self, key: Optional[Callable[[Any], Any]] = None, reverse=False):
"""Sort iterable by *key* function if provided or identity otherwise
Note: sorting loads the entire iterable into memory
>>> flu.sort([3,6,1]).collect()
[1, 3, 6]
>>> flu.sort([3,6,1], reverse=True).collect()
[6, 3, 1]
>>> flu.sort([3,-6,1], key=abs).collect()
[1, 3, -6]
"""
return Fluent(sorted(self, key=key, reverse=reverse))
def take(self, n: Optional[int] = None):
"""Yield first *n* items of the iterable
>>> flu(range(10)).take(2).collect()
[0, 1]
"""
return Fluent(islice(self._iterable, n))
def zip(self, *iterable: Iterable):
"""Yields tuples containing the i-th element from the i-th
argument in the chainable, and the iterable
>>> flu(range(5)).zip(range(3, 0, -1)).collect()
[(0, 3), (1, 2), (2, 1)]
"""
return Fluent(zip(self, *iterable))
or (isinstance(node, str) and not iterate_strings)
or ((base_type is not None) and isinstance(node, base_type))
):
yield node
return
try:
tree = iter(node)
except TypeError:
yield node
return
else:
for child in tree:
for val in walk(child, level + 1):
yield val
return Fluent(walk(self, level=0))
def tee(self, n: int = 2):
"""Return n independent iterators from a single iterable
once tee() has made a split, the original iterable should not be used
anywhere else; otherwise, the iterable could get advanced without the
tee objects being informed
>>> copy1, copy2 = flu(range(5)).tee()
>>> copy1.sum()
10
>>> copy2.collect()
[0, 1, 2, 3, 4]
"""
return Fluent(tee(self, n)).map(Fluent)
def take_while(self, predicate: Callable):
"""Yield elements from the chainable so long as the predicate is true
>>> flu(range(10)).take_while(lambda x: x < 3).collect()
[0, 1, 2]
"""
return Fluent(takewhile(predicate, self._iterable))
def enumerate(self, start: int = 0):
"""Yields tuples from the chainable where the first element
is a count from initial value *start*.
>>> flu(range(5)).zip_longest(range(3, 0, -1)).collect()
[(0, 3), (1, 2), (2, 1), (3, None), (4, None)]
"""
return Fluent(enumerate(self, start=start))