Guest User

Untitled

a guest
Jun 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. l = [ 1, 2, 3 ]
  2. i = l.__iter__()
  3. s = None
  4. while True :
  5. if s :
  6. print s
  7. try :
  8. s = i.next()
  9. except StopIteration :
  10. break
  11. print "done"
  12.  
  13. [stdout:]1
  14. [stdout:]2
  15. [stdout:]3
  16. None['Traceback (most recent call last):
  17. ', ' File "test_python.py", line 8, in <module>
  18. s = i.next()
  19. ', 'StopIteration
  20. ']
  21.  
  22. s = ""
  23. while True :
  24. if state is STATE_CODE :
  25. if "//" in s :
  26. tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
  27. state = STATE_COMMENT
  28. else :
  29. tokens.add( TOKEN_CODE, s )
  30. if state is STATE_COMMENT :
  31. if "//" in s :
  32. tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
  33. else
  34. state = STATE_CODE
  35. # re-evaluate same line
  36. continue
  37. try :
  38. s = i.next()
  39. except StopIteration :
  40. break
  41.  
  42. while True:
  43. stuff()
  44. if fail_condition:
  45. break
  46.  
  47. stuff()
  48. while not fail_condition:
  49. stuff()
  50.  
  51. for i in l:
  52. print i
  53. print "done"
  54.  
  55. for s in l:
  56. while True:
  57. stuff()
  58. # use a "break" instead of s = i.next()
  59.  
  60. for s in some_list:
  61. while True :
  62. if state is STATE_CODE :
  63. if "//" in s :
  64. tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
  65. state = STATE_COMMENT
  66. else :
  67. tokens.add( TOKEN_CODE, s )
  68. if state is STATE_COMMENT :
  69. if "//" in s :
  70. tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
  71. break # get next s
  72. else
  73. state = STATE_CODE
  74. # re-evaluate same line
  75. # continues automatically
  76.  
  77. condition = True
  78. while (condition):
  79. # loop body here
  80. condition = test_loop_condition()
  81. # end of loop
  82.  
  83. try:
  84. while True:
  85. if s:
  86. print s
  87. s = i.next()
  88. except StopIteration:
  89. pass
  90.  
  91. def coroutine(func):
  92. """Coroutine decorator
  93.  
  94. Coroutines must be started, advanced to their first "yield" point,
  95. and this decorator does this automatically.
  96. """
  97. def startcr(*ar, **kw):
  98. cr = func(*ar, **kw)
  99. cr.next()
  100. return cr
  101. return startcr
  102.  
  103. @coroutine
  104. def collector(storage):
  105. """Act as "sink" and collect all sent in @storage"""
  106. while True:
  107. storage.append((yield))
  108.  
  109. @coroutine
  110. def state_machine(sink):
  111. """ .send() new parts to be tokenized by the state machine,
  112. tokens are passed on to @sink
  113. """
  114. s = ""
  115. state = STATE_CODE
  116. while True:
  117. if state is STATE_CODE :
  118. if "//" in s :
  119. sink.send((TOKEN_COMMENT, s.split( "//" )[1] ))
  120. state = STATE_COMMENT
  121. else :
  122. sink.send(( TOKEN_CODE, s ))
  123. if state is STATE_COMMENT :
  124. if "//" in s :
  125. sink.send(( TOKEN_COMMENT, s.split( "//" )[1] ))
  126. else
  127. state = STATE_CODE
  128. # re-evaluate same line
  129. continue
  130. s = (yield)
  131.  
  132. tokens = []
  133. sm = state_machine(collector(tokens))
  134. for piece in i:
  135. sm.send(piece)
  136.  
  137. do {
  138. stuff()
  139. } while (condition())
  140.  
  141. while True:
  142. stuff()
  143. if not condition():
  144. break
  145.  
  146. def do_while(stuff, condition):
  147. while condition(stuff()):
  148. pass
  149.  
  150. while condition is True:
  151. stuff()
  152. else:
  153. stuff()
  154.  
  155. loop = True
  156. while (loop):
  157. generic_stuff()
  158. try:
  159. questionable_stuff()
  160. # to break from successful completion
  161. # loop = False
  162. except:
  163. optional_stuff()
  164. # to break from unsuccessful completion -
  165. # the case referenced in the OP's question
  166. loop = False
  167. finally:
  168. more_generic_stuff()
  169.  
  170. while (True):
  171. generic_stuff()
  172. try:
  173. questionable_stuff()
  174. # to break from successful completion
  175. # break
  176. except:
  177. optional_stuff()
  178. # to break from unsuccessful completion -
  179. # the case referenced in the OP's question
  180. break
  181.  
  182. flagBreak = false;
  183. while True :
  184.  
  185. if(flagBreak ) break;
  186.  
  187. if s :
  188. print s
  189. try :
  190. s = i.next()
  191. except StopIteration :
  192. flagBreak = true;
  193. print "done"
  194.  
  195. for s in l :
  196. print s
  197. print "done"
Add Comment
Please, Sign In to add comment