root/lib/iters.py @ 1885
1 |
# Iterators and iterables
|
---|---|
2 |
|
3 |
def is_iterable(value): return getattr(value, '__iter__', False) |
4 |
|
5 |
def flatten(outer_iter): |
6 |
for iter_ in outer_iter: |
7 |
for el in iter_: yield el |
8 |
|
9 |
def flatten_n(outer_iter, depth=2): |
10 |
if depth < 2: return outer_iter |
11 |
elif depth == 2: return flatten(outer_iter) |
12 |
else: return flatten(flatten_n(outer_iter, depth-1)) |