Reputation: 4415
How do you programmatically stop a python script after a condition sentence has run through. In the pseudo script below:
for row in rows:
if row.FIRSTDATE == row.SECONDDATE:
pass
else:
print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
## If I set my quit sequence at the this tab level, it quits after the first
## unmatched record is found. I don't want that, I want it to quit after all the
## unmatched records have been found, if any. if all records match, I want the
## script to continue and not quit
sys.quit("Ending Script")
Thanks, Mike
Upvotes: 0
Views: 5817
Reputation: 174698
Another approach:
mis_match = []
for row in rows:
if row.FIRSTDATE != row.SECONDDATE:
mis_match.append(row.UNIQUEID)
if mis_match:
print "The following rows didn't match" + '\n'.join(mis_match)
sys.exit()
Upvotes: 1
Reputation: 771
I would do it like this:
def DifferentDates(row):
if row.FIRSTDATE != row.SECONDDATE:
print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
return True
else:
return False
# Fill a list with Trues and Falses, using the check above
checked_rows = map(DifferentDates, rows)
# If any one row is different, sys exit
if any(checked_rows):
sys.exit()
Documentation for any
Upvotes: 1
Reputation: 114048
not sure if i understand correctly
doQuit = 0
for row in rows:
if row.FIRSTDATE != row.SECONDDATE:
print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
doQuit = 1
if doQuit: sys.exit()
Upvotes: 1
Reputation: 9597
quit_flag = False
for row in rows:
if row.FIRSTDATE == row.SECONDDATE:
pass
else:
print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
quit_flag = True
if quit_flag:
print "Ending Script"
sys.exit()
Upvotes: 2