Writing a convenient and decent-looking wrapper for working with transactions is a favorite pastime for many developers. Transactions must be able to start, finish and rollback properly, and they can also be nested. Python is a multifaceted language (or, as speechwriters say, “Multi-paradigm”), giving us innumerable ways of doing it.
As a foundation, I usually use Python DB API (namely, psycopg2
module). The API itself is rather low-level and is crying for improvement. What can be done?
Initially, let’s just make an ordinary decorator for opening and closing a transaction:
(more…)