Re: Use of context managers with DB API 2

Vernon Cole <[email protected]>
Newsgroups gmane.comp.python.db
Message-ID <CAH-ZgAetaHXU+m22fAfOr=48cDmVXcz+tRgXENjEzzJptSs8Jg@mail.gmail.com>
Christoph, I think you may be Dutch -- your design proposal feels right to
me.
(+1) from Vernon

On Fri, Nov 2, 2012 at 3:53 AM, Christoph Zwerschke <[email protected]> wrote:

>
> What I find a bit ugly about this approach is that now the context
> managers of connections and cursors behave differently, and that code
> written using these context managers is not self-explanatory. If
> connections and cursors would just have closing context managers, and
> connections had an extra "transaction" member that is a context manager for
> handling the transaction, the code would be more readable and explicit
> which is better than implicit:
>
>     with dbapi2.connect(...) as con:
>         with con.transaction:
>             with con.cursor() as cur:
>                 cur.execute("insert into debit(amount) values (-100)")
>                 cur.execute("insert into credit(amount) values (100)")
>         with con.transaction:
>             with con.cursor() as cur:
>                 cur.execute("insert into debit(amount) values (-200)")
>                 cur.execute("insert into credit(amount) values (200)")
>
> Or, when using the shortcut methods:
>
>     with dbapi2.connect(...) as con:
>         with con.transaction:
>             con.execute("insert into debit(amount) values (-100)")
>             con.execute("insert into credit(amount) values (100)")
>         with con.transaction:
>             con.execute("insert into debit(amount) values (-200)")
>             con.execute("insert into credit(amount) values (200)")
>
>

_______________________________________________
DB-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/db-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.