Ich kann sie nicht zu Nicht-Generaor-Funktionen machen, da ich in anderen Anwendungsfällen auf jeden Fall die zurückgegebenen Werte benötige.
Ich verwende derzeit eine triviale selbst erstellte Funktion, um die Generatoren auszuschöpfen.
Code: Select all
def exhaust(generator):
for _ in generator:
pass
Nach einem Anwendungsfall:
Code: Select all
def create_tables(fail_silently=True):
"""Create the respective tables."""
for model in MODELS:
try:
model.create_table(fail_silently=fail_silently)
except Exception:
yield (False, model)
else:
yield (True, model)
Code: Select all
for success, table in create_tables():
if success:
print('Creation of table {} succeeded.'.format(table))
else:
print('Creation of table {} failed.'.format(table), file=stderr)
Code: Select all
exhaust(create_tables())
Mobile version