Emulate a do-while loop in Python?

后端 未结 16 900
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:47

I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:

list_of_ints = [ 1, 2, 3 ]
iterator =         


        
相关标签:
16条回答
  • 2020-11-22 07:27

    The way I've done this is as follows...

    condition = True
    while condition:
         do_stuff()
         condition = (<something that evaluates to True or False>)
    

    This seems to me to be the simplistic solution, I'm surprised I haven't seen it here already. This can obviously also be inverted to

    while not condition:
    

    etc.

    0 讨论(0)
  • 2020-11-22 07:28

    If you're in a scenario where you are looping while a resource is unavaliable or something similar that throws an exception, you could use something like

    import time
    
    while True:
        try:
           f = open('some/path', 'r')
        except IOError:
           print('File could not be read. Retrying in 5 seconds')   
           time.sleep(5)
        else:
           break
    
    0 讨论(0)
  • 2020-11-22 07:30

    For me a typical while loop will be something like this:

    xBool = True
    # A counter to force a condition (eg. yCount = some integer value)
    
    while xBool:
        # set up the condition (eg. if yCount > 0):
            (Do something)
            yCount = yCount - 1
        else:
            # (condition is not met, set xBool False)
            xBool = False
    

    I could include a for..loop within the while loop as well, if situation so warrants, for looping through another set of condition.

    0 讨论(0)
  • 2020-11-22 07:37

    I am not sure what you are trying to do. You can implement a do-while loop like this:

    while True:
      stuff()
      if fail_condition:
        break
    

    Or:

    stuff()
    while not fail_condition:
      stuff()
    

    What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:

    for i in l:
      print i
    print "done"
    

    Update:

    So do you have a list of lines? And you want to keep iterating through it? How about:

    for s in l: 
      while True: 
        stuff() 
        # use a "break" instead of s = i.next()
    

    Does that seem like something close to what you would want? With your code example, it would be:

    for s in some_list:
      while True:
        if state is STATE_CODE:
          if "//" in s:
            tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
            state = STATE_COMMENT
          else :
            tokens.add( TOKEN_CODE, s )
        if state is STATE_COMMENT:
          if "//" in s:
            tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
            break # get next s
          else:
            state = STATE_CODE
            # re-evaluate same line
            # continues automatically
    
    0 讨论(0)
  • 2020-11-22 07:37
    do {
      stuff()
    } while (condition())
    

    ->

    while True:
      stuff()
      if not condition():
        break
    

    You can do a function:

    def do_while(stuff, condition):
      while condition(stuff()):
        pass
    

    But 1) It's ugly. 2) Condition should be a function with one parameter, supposed to be filled by stuff (it's the only reason not to use the classic while loop.)

    0 讨论(0)
  • 2020-11-22 07:38

    Here is a crazier solution of a different pattern -- using coroutines. The code is still very similar, but with one important difference; there are no exit conditions at all! The coroutine (chain of coroutines really) just stops when you stop feeding it with data.

    def coroutine(func):
        """Coroutine decorator
    
        Coroutines must be started, advanced to their first "yield" point,
        and this decorator does this automatically.
        """
        def startcr(*ar, **kw):
            cr = func(*ar, **kw)
            cr.next()
            return cr
        return startcr
    
    @coroutine
    def collector(storage):
        """Act as "sink" and collect all sent in @storage"""
        while True:
            storage.append((yield))
    
    @coroutine      
    def state_machine(sink):
        """ .send() new parts to be tokenized by the state machine,
        tokens are passed on to @sink
        """ 
        s = ""
        state = STATE_CODE
        while True: 
            if state is STATE_CODE :
                if "//" in s :
                    sink.send((TOKEN_COMMENT, s.split( "//" )[1] ))
                    state = STATE_COMMENT
                else :
                    sink.send(( TOKEN_CODE, s ))
            if state is STATE_COMMENT :
                if "//" in s :
                    sink.send(( TOKEN_COMMENT, s.split( "//" )[1] ))
                else
                    state = STATE_CODE
                    # re-evaluate same line
                    continue
            s = (yield)
    
    tokens = []
    sm = state_machine(collector(tokens))
    for piece in i:
        sm.send(piece)
    

    The code above collects all tokens as tuples in tokens and I assume there is no difference between .append() and .add() in the original code.

    0 讨论(0)
提交回复
热议问题