Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def flat_sequnce_gen(input_list, traverse=None, exclude=None, debug=False):
- traverse = traverse or (list, tuple)
- exclude = exclude or (str, bytes, int, float, complex)
- iterators = [iter(input_list)]
- last = None
- if debug:
- exhausted = []
- while iterators:
- if debug:
- time.sleep(0.1)
- try:
- current = next(iterators[-1])
- except StopIteration:
- it = iterators.pop()
- if debug:
- print('Stop iteration.')
- exhausted.append(it)
- continue
- if isinstance(current, traverse) and not isinstance(current, exclude):
- if last is current:
- if debug:
- print('Last is Current', last)
- yield current
- iterators.pop()
- continue
- if debug:
- print('Traverse', current)
- iterators.append(iter(current))
- last = current
- else:
- if debug:
- print('Yielding', current)
- yield current
- if debug:
- print('Yielding exhausted iterators')
- yield exhausted
Advertisement
Add Comment
Please, Sign In to add comment