Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50102

Re: Default scope of variables

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.088
X-Spam-Evidence '*H*': 0.83; '*S*': 0.01; 'logic': 0.09; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ideal.': 0.16; 'magic': 0.16; 'nest': 0.16; 'wayne': 0.16; 'wrote:': 0.18; 'work,': 0.20; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'this.': 0.32; 'skip:d 20': 0.34; 'transaction': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'though,': 0.39; 'to:addr:python.org': 0.39; 'back': 0.62; 'kind': 0.63; 'jul': 0.74; "'with'": 0.84; 'hardly': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=uFVA2UhcuD6mr3R/5GpZ7Nobmh7MX1Xz6EpRTtN1e9k=; b=GZcet8zdGSMF/nQNJGFgvoCjpQa6alZlxoEdsFnqGZGHy4z6fo9a4/CtbGZ1+SjUV9 Yk7mPCZ7oe/muIzSwXjwoHqF6sY1O5PQRClwn1eZoNStWd0s5E67Tji4pGi/xGqdEyny 5tHly1Lt3rjTdJgvHTHJrTR1WsRhTTNoSWTgP9A4ZqTzoqVXuURtsW4ChCxT99UxgDcd K3x2pfAJ4cXDTXuypH8qN/0sMZnvPJgLgrQa8K+PmhEjYrfSxvtwg9HWuWSUNUtTUapC m5wJOtq/oUEE7ArgKsSEa0WwtY4BOWiS1EYwCWG2deBomEqqvJsnl39SJjYOgPyhGEf4 kHKA==
MIME-Version 1.0
X-Received by 10.58.187.4 with SMTP id fo4mr11765552vec.55.1373204604494; Sun, 07 Jul 2013 06:43:24 -0700 (PDT)
In-Reply-To <alpine.DEB.2.02.1307070759350.18702@gilgamesh>
References <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com> <mailman.4200.1372910878.3114.python-list@python.org> <51d508ed$0$6512$c3e8da3$5496439d@news.astraweb.com> <mailman.4208.1372916885.3114.python-list@python.org> <51d5a504$0$29999$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmpgmABYwn0xB4T-9VbYMP=8JfpRcy8TPSerzyGvzPbMmA@mail.gmail.com> <alpine.DEB.2.02.1307070759350.18702@gilgamesh>
Date Sun, 7 Jul 2013 23:43:24 +1000
Subject Re: Default scope of variables
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4357.1373204606.3114.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373204606 news.xs4all.nl 15975 [2001:888:2000:d::a6]:46382
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50102

Show key headers only | View raw


On Sun, Jul 7, 2013 at 11:13 PM, Wayne Werner <wayne@waynewerner.com> wrote:
> Which you would then use like:
>
>
> conn = create_conn()
> with new_transaction(conn) as tran:
>      rows_affected = do_query_stuff(tran)
>      if rows_affected == 42:
>           tran.commit()

Yep. There's a problem, though, when you bring in subtransactions. The
logic wants to be like this:

with new_transaction(conn) as tran:
    tran.query("blah")
    with tran.subtransaction() as tran:
        tran.query("blah")
        with tran.subtransaction() as tran:
            tran.query("blah")
            # roll this subtransaction back
        tran.query("blah")
        tran.commit()
    tran.query("blah")
    tran.commit()

The 'with' statement doesn't allow this. I would need to use some kind
of magic to rebind the old transaction to the name, or else use a list
that gets magically populated:

with new_transaction(conn) as tran:
    tran[-1].query("blah")
    with subtransaction(tran):
        tran[-1].query("blah")
        with subtransaction(tran):
            tran[-1].query("blah")
            # roll this subtransaction back
        tran[-1].query("blah")
        tran[-1].commit()
    tran[-1].query("blah")
    tran[-1].commit()

I don't like the look of this. It might work, but it's hardly ideal.
This is why I like to be able to nest usages of the same name.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 03:27 +0000
  Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 14:07 +1000
    Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 05:32 +0000
      Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 15:47 +1000
        Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 16:38 +0000
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-05 02:58 +1000
          Re: Default scope of variables Wayne Werner <wayne@waynewerner.com> - 2013-07-07 08:13 -0500
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-07 23:43 +1000
            Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-07 16:03 +0000
              Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 10:46 +1000
            Re: Default scope of variables alex23 <wuwei23@gmail.com> - 2013-07-09 14:52 +1000
              Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 15:07 +1000
                Re: Default scope of variables alex23 <wuwei23@gmail.com> - 2013-07-09 16:08 +1000
                Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 16:13 +1000
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 09:35 +0200
                Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 17:45 +1000
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 01:56 -0600
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 10:22 +0200
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 10:38 +0200
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 09:07 -0700
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 10:44 -0600
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 10:23 -0700
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 12:41 -0600
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 12:19 -0700
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-10 07:54 +0200
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-10 09:42 -0600
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-10 08:29 -0700
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-11 07:52 +0200
          Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-07 09:52 -0700
          Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-07 11:59 -0700
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 10:48 +1000
            Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-08 02:23 +0000
              Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 13:11 +1000
                Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-08 05:00 +0000
                Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 15:14 +1000
      Re: Default scope of variables Peter Otten <__peter__@web.de> - 2013-07-04 08:48 +0200
      Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-04 01:12 -0600
      Re: Default scope of variables Dave Angel <davea@davea.name> - 2013-07-04 03:06 -0400
        Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 15:52 +0000
        Re: Default scope of variables Rotwang <sg552@hotmail.co.uk> - 2013-07-04 17:54 +0100
          Re: Default scope of variables Peter Otten <__peter__@web.de> - 2013-07-04 20:36 +0200
          Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 01:04 +0100
          Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 01:24 +0000
            Re: Default scope of variables Dave Angel <davea@davea.name> - 2013-07-04 22:03 -0400
            Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 03:29 +0100
            Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 03:27 +0100
            Re: Default scope of variables Rotwang <sg552@hotmail.co.uk> - 2013-07-05 07:28 +0100
        Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-05 13:24 +0000
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-05 23:43 +1000
            Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-05 15:36 +0000
          Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-07 16:08 +0000
            Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-08 11:54 +0000
              Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 14:14 +0100
      Re: Default scope of variables Lele Gaifax <lele@metapensiero.it> - 2013-07-04 14:43 +0200
      Re: Default scope of variables Wayne Werner <wayne@waynewerner.com> - 2013-07-04 10:45 -0500
  Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 05:30 +0100
    Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 05:45 +0000
  Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 14:36 +1000
  Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 06:09 +0100
  Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 17:58 +0100

csiph-web