2

I am working on something that needs to make it's way through several levels of checking if conditions are met before either exiting all together or setting certain variables and then starting the loop over. My question revolves around how to jump back to the primary while loop from the internal for loop.

while True:
    message = stomp.get
    message = simplejson.loads(message.body)

    if message[0]['fieldname1'] == False:
      global ShutdownState
      ShutdownState = True
      break # Should leave the While loop all together

    else:

      for item in message[0]['fieldname2'][0]['fieldname2-1']

        if item['fieldname2-1-1'] == True:
           list1_new[len(list_new):] = [item['fieldname2-1-2']
           list1-state = set(list1) == set(list1_new)

           if list1-state == True:
               continue # should reset the while loop

        else:
           list1 = list1_new # should print the new list1 and then reset the while loop
           print list1
           continue
3
  • 6
    In this case it looks like you can use break in place of continue to break out of the for loop and stay in the while loop. However I think you could avoid the problem with better structure. It's a little hard to tell what's going on though with these variables names. Commented Jan 31, 2015 at 5:49
  • @cdarke sorry, pasting it in screwed it up. Commented Jan 31, 2015 at 5:59
  • 1
    Is this (conceptually) the whole loop, or is there more following it? If this is the whole thing, then some easy cleanups are things like: "in the main [if test then A else B], the then part ends with break. That could easily be refactored to just: [if test then A] [B]" which brings the whole B part out a level. Commented Jan 31, 2015 at 12:26

1 Answer 1

2

It's not clear whether the sample code was meant to be representative of your whole loop, or just the beginning of it. If it was the whole thing, then there are lots of ways you can restructure it. Here's a first stab (note that are some typos in the code (e.g., list1-state rather than list1_state, and things like that), so I've had to adjust some things. You'll need to check whether it still matches up with your original code. (For more about this implementation of finding the first element in the list, and some alternatives, have a look at Python: Find in list.)

while True:
    message = stomp.get
    message = simplejson.loads(message.body)

    # If the message doesn't match this criterion,
    # we need to abort everything.
    if not message[0]['fieldname1']:
        global ShutdownState
        ShutdownState = True
        break

    try:
        # get the first item in message[0]['fieldname2'][0]['fieldname2-1']
        # such item['fieldname2-1-1'] is true.  Whether we
        # find one and do this code, or don't and catch the
        # StopIteration, we wrap back to the while loop.
        item = next(x
                    for x in message[0]['fieldname2'][0]['fieldname2-1']
                    if item['fieldname2-1-1'])
        list1_new[len(list_new),:] = item['fieldname2-1-2']
        list1_state = (set(list1) == set(list1_new))

        if not list1_state:
            list1 = list1_new # should print the new list1 and then reset the while loop
            print list1
    except StopIteration:
        # There was no such item.
        pass

You might also clean this up by making it a do-while loop, but that's a less major factor. Based on Emulate a do-while loop in Python?, you could do something like:

def get_message():
    message = stomp.get
    return simplejson.loads(message.body)

message = get_message()
while message[0]['fieldname1']:
    try:
        # get the first item in message[0]['fieldname2'][0]['fieldname2-1']
        # such item['fieldname2-1-1'] is true.  Whether we
        # find one and do this code, or don't and catch the
        # StopIteration, we wrap back to the while loop.
        item = next(x
                    for x in message[0]['fieldname2'][0]['fieldname2-1']
                    if item['fieldname2-1-1'])
        list1_new[len(list_new),:] = item['fieldname2-1-2']
        list1_state = (set(list1) == set(list1_new))

        if not list1_state:
            list1 = list1_new # should print the new list1 and then reset the while loop
            print list1
    except StopIteration:
        # There was no such item.
        pass
    message = get_message()

global ShutdownState
ShutdownState = True
Sign up to request clarification or add additional context in comments.

1 Comment

thank you your first example cleans things up to a much more manageable level

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.