SHARE
TWEET

deep_iter3

DeaD_EyE Feb 10th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. """
  3. Thread: https://python-forum.io/Thread-Cheat-against-recursion-limit
  4. Have fun...
  5. """
  6.  
  7. import sys
  8. from collections import deque
  9.  
  10.  
  11. def construct_deep_list(n):
  12.     x = y = []
  13.     for _ in range(n):
  14.         x.append([])
  15.         x = x[0]
  16.     x.append('Secret')
  17.     return y
  18.  
  19.  
  20. def deep_iter3(iterable, ignore=(str, bytes)):
  21.     heap = deque([iterable])
  22.     while heap:
  23.         element = heap.popleft()
  24.         if isinstance(element, ignore):
  25.             yield element
  26.             continue
  27.         try:
  28.             sub_heap = []
  29.             for sub_element in element:
  30.                 sub_heap.append(sub_element)
  31.         except TypeError:
  32.             yield element
  33.         else:
  34.             heap.extendleft(reversed(sub_heap))
  35.  
  36.  
  37. deep_list = construct_deep_list(sys.getrecursionlimit())
  38. mixed_list = [1,2, ['test'], (3,1),4, deep_list]
  39. result = list(deep_iter3(mixed_list))
  40. print(result)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top