1

I need some help here.

I have this for loop

for i in range(0,15):
        logging.info('-----  packing iteration %i started  ------' % (i))
        po_selection_list = po_sel.get_po_lists(TrayID,TrayHeight,Materials,TrayName,i) # get po lists for this iteration
        po_count = 0
        for po_list in po_selection_list: # loop through all lists
            if len(po_list) == 0: #if list is empty go to next list
                pass
            else: # else load po's in tray
                po_count += len(po_list)
                functions_library.AddPOs_Stage1(po_list,driver)
                functions_library.AddPOs_trayIDSearch(driver)
                functions_library.AddPOs_Stage2(driver)

                functions_library.ImportParts()
        if po_count == 0: # if no po's were loaded in the tray go to next iteration
            pass
        else: # else pack and sync in netfabb
            functions_library.MovePartsZHeight()
            functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
            functions_library.RemoveNetfabbExtraParts()
            functions_library.NetfabbSync(driver)

Imagine if we are running in iteration number 4. What I need is if functions_library.AddPOs_trayIDSearch(driver) returns False I want to restart the loop again (iteration number 4)

EDIT

for i in range(0,15):
    logging.info('-----  packing iteration %i started  ------' % (i))
    po_selection_list = po_sel.get_po_lists(TrayID,TrayHeight,Materials,TrayName,i) # get po lists for this iteration
    po_count = 0
    for po_list in po_selection_list: # loop through all lists
        if len(po_list) == 0: #if list is empty go to next list
            pass
        else: # else load po's in tray
            po_count += len(po_list)
            functions_library.AddPOs_Stage1(po_list,driver)
            functions_library.AddPOs_trayIDSearch(driver)
            functions_library.AddPOs_Stage2(driver)

            functions_library.ImportParts()
    if po_count: # if no po's were loaded in the tray go to next iteration
        functions_library.MovePartsZHeight()
        functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
        functions_library.RemoveNetfabbExtraParts()
        functions_library.NetfabbSync(driver)

### finish the tray ###
functions_library.SelectAll(TrayHeight)
functions_library.MovePartsZHeight()
functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
functions_library.RemoveNetfabbExtraParts()
functions_library.RemoveCylinders(TrayHeight)
functions_library.NetfabbSync(driver)
functions_library.SetToPrinting(driver)
functions_library.SaveJob(tray_folder,TrayName)
functions_library.NetfabbSlicing(TrayID,tray_folder,TrayName)
logging.info('Tray: '+TrayName+' done!')
3
  • Are you maybe looking for continue? docs.python.org/2/tutorial/… Commented Sep 29, 2015 at 9:45
  • No because I don't want to skip to iteration 5, I want to re-run iteration 4. Commented Sep 29, 2015 at 9:51
  • I don't see where the variable driver comes from. Does it change its value every iteration? The code example as provided doesn't make sense, because if the value for driver doesn't change you'll create an endless loop, iterating all the time for 4. Commented Sep 29, 2015 at 9:56

2 Answers 2

2

Before answering, note that:

if something == 0:
    pass
else:
    do_stuff()

can be simplified into:

if something:
    do_stuff()

because Python interprets it as if bool(something) == True: do_stuff().

For your actual question, you could use a while loop and increment a counter only if the call you’re interested in returns True. You can check that every calls succeded using the for ... else construct:

i = 0
while i < 15:
    logging.info('-----  packing iteration %i started  ------' % (i))
    po_selection_list = po_sel.get_po_lists(TrayID,TrayHeight,Materials,TrayName,i) # get po lists for this iteration
    po_count = 0
    for po_list in po_selection_list: # loop through all lists
        if po_list: # load po's in tray
            po_count += len(po_list)
            functions_library.AddPOs_Stage1(po_list,driver)
            if not functions_library.AddPOs_trayIDSearch(driver):
                break
            functions_library.AddPOs_Stage2(driver)

            functions_library.ImportParts()
    else:
        # break didn't occur
        if po_count: # pack and sync in netfabb
            functions_library.MovePartsZHeight()
            functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
            functions_library.RemoveNetfabbExtraParts()
            functions_library.NetfabbSync(driver)
        i += 1
Sign up to request clarification or add additional context in comments.

7 Comments

It can be simplified to if a != 0: do_stuff(). You have a slight error in the beginning.
@cezar if a: do_stuff() is more generic than if a != 0: do_stuff() as it accounts for all truthy values, not only integer ones. Check the if po_list in the code to understand.
I meant something, just took a as shortcut. In order to simplify the if-clause you have to negate the condition.
Yes, you're right. When comparing to 0, we can check for truthy (or falsy) values. I was thinking in general, not concerning the 0 in particular. But that pass in the if-clause immediately caught my eye. It definitely doesn't belong there. It's very good to point that out.
The problem of this solution is that I would break the for po_list only. I also want the while loopto stop that iteration and start again. I think this wasn't totally clear because I missed some of the code. I will edit the post now
|
0

Is it not easier to use while iterator?

c = 0
while c < 5:
    ...
    result = functions_library.AddPOs_trayIDSearch(...)
    if not result:
        c = 0
    else:
        c -= 1

Comments

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.