4
  • 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)

5 Answers 5

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

Looks like my code could improve a lot using the with-statement.
Recently, there is another similar question to this one, however sufficiently different in that question is a more simplified and generalized form, and given this answer recommends usage of a context manager, but lacking an example, I provided an example by giving an answer in that same thread, with code inspired by the question here.
3

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

2 Comments

successCount = len(rows) - errorCount: doesn't always work in my example as I don't always work directly with rows.
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.
2

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.

Comments

0

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

Comments

0

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.

1 Comment

I think you have misunderstood my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.