SHOW:
|
|
- or go back to the newest paste.
| 1 | - | def flat_sequnce_gen(input_list, traverse=None, exclude=None, debug=False): |
| 1 | + | def flat_sequnce_gen(input_list, traverse=None, exclude=None, debug=False): |
| 2 | - | traverse = traverse or (list, tuple) |
| 2 | + | traverse = traverse or (list, tuple) |
| 3 | - | exclude = exclude or (str, bytes, int, float, complex) |
| 3 | + | exclude = exclude or (str, bytes, int, float, complex) |
| 4 | - | iterators = [iter(input_list)] |
| 4 | + | iterators = [iter(input_list)] |
| 5 | - | last = None |
| 5 | + | last = None |
| 6 | - | if debug: |
| 6 | + | while iterators: |
| 7 | - | exhausted = [] |
| 7 | + | try: |
| 8 | - | while iterators: |
| 8 | + | current = next(iterators[-1]) |
| 9 | - | if debug: |
| 9 | + | except StopIteration: |
| 10 | - | time.sleep(0.1) |
| 10 | + | iterators.pop() |
| 11 | - | try: |
| 11 | + | continue |
| 12 | - | current = next(iterators[-1]) |
| 12 | + | if isinstance(current, traverse) and not isinstance(current, exclude): |
| 13 | - | except StopIteration: |
| 13 | + | if last is current: |
| 14 | - | it = iterators.pop() |
| 14 | + | yield current |
| 15 | - | if debug: |
| 15 | + | iterators.pop() |
| 16 | - | print('Stop iteration.') |
| 16 | + | continue |
| 17 | - | exhausted.append(it) |
| 17 | + | iterators.append(iter(current)) |
| 18 | - | continue |
| 18 | + | last = current |
| 19 | - | if isinstance(current, traverse) and not isinstance(current, exclude): |
| 19 | + | else: |
| 20 | - | if last is current: |
| 20 | + |