root/lib/iters.py @ 1885
1 | 1881 | aaronmk | # Iterators and iterables
|
---|---|---|---|
2 | 1711 | aaronmk | |
3 | 1881 | aaronmk | def is_iterable(value): return getattr(value, '__iter__', False) |
4 | |||
5 | 1711 | aaronmk | 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)) |