views:

323

answers:

5
  • I'd like to avoid writting errorCount += 1 in more than one place.
  • I'm looking for a better way than
    success = False
    try:
        ...
    else:
        success = True
    finally:
        if success:
            storage.store.commit()
        else:
            storage.store.rollback()
  • I'm trying to avoid store.rollback() in every except clause.

Any idea on how to do this?

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)
+2  A: 

You could simply wrap up your exception implementation inside an exception specific container class, that way you could also avoid all these explicit print calls (which may come in handy once you change your interface, e.g. when supporting a GUI), instead you would have a method like error(msg), which in turn could internally increase the error count accordingly. In other words, simply set up an external helper class that manages your exception handling stuff.

none
A: 

If you like cumulate the errors why you don't cumulate the errors? If you put the error messages on a list the size of the list gives the information you need. You can even postprocess something. You can decide easy if an error occured and print is called only at a single place

Norbert Hartl
A: 

Well, according to this page, part 7.4:

http://docs.python.org/reference/compound_stmts.html

This is possible with python ver. >= 2.6. I mean try..except..finally construction.

Marcin Cylke
I think you have misunderstood my question.
Georg
+3  A: 

My suggestion would to write an logError() method that increments errorCount (make it a member variable) and prints the error. Since your exception code is similar, you could also shorten your code by doing this:

try:
    # something
except (CustomerNotFoundError, ProductNotFoundError), e:
    logError(e)

You can print whatever you want based on e.

Also, you don't need to track succeses: successCount = len(rows) - errorCount

jcoon
successCount = len(rows) - errorCount: doesn't always work in my example as I don't always work directly with rows.
Georg
There are many good reasons to make a single method that processes exception objects, and call it whenever your code catches an exception that's an error.
Robert Rossney
+8  A: 

This look like a possible application of Python's new with statement. It allows to to unwind operations and release resources securely no matter what outcome a block of code had.

Read about it in PEP 343

Ber
Looks like my code could improve a lot using the with-statement.
Georg