Guest User

Untitled

a guest
May 25th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. def flat_sequnce_gen(input_list, traverse=None, exclude=None, debug=False):
  2. traverse = traverse or (list, tuple)
  3. exclude = exclude or (str, bytes, int, float, complex)
  4. iterators = [iter(input_list)]
  5. last = None
  6. if debug:
  7. exhausted = []
  8. while iterators:
  9. if debug:
  10. time.sleep(0.1)
  11. try:
  12. current = next(iterators[-1])
  13. except StopIteration:
  14. it = iterators.pop()
  15. if debug:
  16. print('Stop iteration.')
  17. exhausted.append(it)
  18. continue
  19. if isinstance(current, traverse) and not isinstance(current, exclude):
  20. if last is current:
  21. if debug:
  22. print('Last is Current', last)
  23. yield current
  24. iterators.pop()
  25. continue
  26. if debug:
  27. print('Traverse', current)
  28. iterators.append(iter(current))
  29. last = current
  30. else:
  31. if debug:
  32. print('Yielding', current)
  33. yield current
  34. if debug:
  35. print('Yielding exhausted iterators')
  36. yield exhausted
Advertisement
Add Comment
Please, Sign In to add comment