I was looking at this SO question about the Python For Else flow control and I thought I had a portion of code that which is very close to the example given. I think that my code is very close to the example code except that I want the for loop to finish (for logging purposes).
for module_name, class_name in BASIC_PARSERS_TO_RUN:
full_module_name = "parsers." + module_name
parser = getattr(import_module(full_module_name), class_name)(
logger=logger)
parser_data = parser.parse(cluster_path)
if parser_data is None or parser_data == "0":
# Basic None instead of "None" so that json is serialized to null
# instead of "None"
json_data_list.append({module_name: parser_data})
failed_basic_checks = True
else:
json_data_list.append({module_name: str(parser_data)})
# Checking if we have a valid data set.
if failed_basic_checks:
json_data_list.append({"basic_validation_succeeded": False})
return json.dumps(json_data_list)
# We've run into a dataset which isn't valid.
exit(1)
Is there any way to change my for loop to use the for else flow control?
found_obj = None
for obj in objects:
if obj.key == search_key:
found_obj = obj
break
else:
print 'No object found.'