errorCount += 1
複数の場所に書き込むことは避けたいと思います。- 私はより良い方法を探しています
成功 = 偽 試す: ... そうしないと: 成功 = 真 最後に: 成功した場合: storage.store.commit() そうしないと: storage.store.rollback()
- 私は
store.rollback()
すべてのexcept節で避けようとしています。
これを行う方法について何か考えはありますか?
count = 0
successCount = 0
errorCount = 0
for row in rows:
success = False
count += 1
newOrder = storage.RepeatedOrder()
storage.store.add(newOrder)
try:
try:
newOrder.customer = customers[row.customer_id]
except KeyError:
raise CustomerNotFoundError, (row.customer_id,)
newOrder.nextDate = dates[row.weekday]
_fillOrder(newOrder, row.id)
except CustomerNotFoundError as e:
errorCount += 1
print u"Error: Customer not found. order_id: {0}, customer_id: {1}".format(row.id, e.id)
except ProductNotFoundError as e:
errorCount += 1
print u"Error: Product not found. order_id: {0}, product_id: {1}".format(row.id, e.id)
else:
success = True
successCount += 1
finally:
if success:
storage.store.commit()
else:
storage.store.rollback()
print u"{0} of {1} repeated orders imported. {2} error(s).".format(successCount, count, errorCount)