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


Groups > comp.lang.python > #50221

Re: Default scope of variables

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'context': 0.07; 'method.': 0.07; 'nested': 0.07; 'none:': 0.07; 'variables': 0.07; 'activity.': 0.09; 'assumed': 0.09; 'happen,': 0.09; 'instance.': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type,': 0.09; 'yeah,': 0.09; 'def': 0.12; 'wrote': 0.14; 'block.': 0.16; 'executed,': 0.16; 'interest,': 0.16; 'other,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reraise': 0.16; 'skip:n 70': 0.16; 'timestamp': 0.16; 'top-level': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'do.': 0.18; '>>>': 0.22; 'separate': 0.22; 'sorry,': 0.24; 'equivalent': 0.26; 'query': 0.26; 'updating': 0.26; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'related': 0.29; 'code': 0.31; 'piece': 0.31; 'class': 0.32; 'probably': 0.32; 'skip:c 30': 0.32; 'option': 0.32; 'skip:_ 10': 0.34; 'received:co.za': 0.34; 'received:za': 0.34; "i'd": 0.34; 'common': 0.35; 'connection': 0.35; 'created': 0.35; 'transaction': 0.35; 'case,': 0.35; 'objects': 0.35; 'there': 0.35; 'false': 0.36; 'doing': 0.36; 'manager': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'even': 0.60; 'access,': 0.60; 'transaction.': 0.60; 'within': 0.65; 'effectively': 0.66; 'frank': 0.68; 'received:41': 0.70; 'jul': 0.74; 'shadow': 0.84; 'technique.': 0.84; 'tricky': 0.84; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Re: Default scope of variables
Date Tue, 9 Jul 2013 09:35:34 +0200
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><mailman.4357.1373204606.3114.python-list@python.org><krg4k4$tea$1@dont-email.me><mailman.4423.1373346452.3114.python-list@python.org><krg91s$e6j$1@dont-email.me> <CAPTjJmqkmFd4-Jpugr-vubuB6riBV6K_Mwnxc_U3CVaBr_Wgbg@mail.gmail.com>
X-Gmane-NNTP-Posting-Host 41-133-114-204.dsl.mweb.co.za
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-RFC2646 Format=Flowed; Original
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4913
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.4435.1373355344.3114.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373355344 news.xs4all.nl 15915 [2001:888:2000:d::a6]:59868
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50221

Show key headers only | View raw


"Chris Angelico" <rosuav@gmail.com> wrote in message 
news:CAPTjJmqkmFd4-Jpugr-vubuB6riBV6K_Mwnxc_U3CVaBr_Wgbg@mail.gmail.com...
> On Tue, Jul 9, 2013 at 4:08 PM, alex23 <wuwei23@gmail.com> wrote:
>> On 9/07/2013 3:07 PM, Chris Angelico wrote:
>>>
>>> The subtransactions are NOT concepted as separate transactions. They
>>> are effectively the database equivalent of a try/except block.
>>
>>
>> Sorry, I assumed each nested query was somehow related to the prior
>> one. In which case, I'd probably go with Ethan's suggestion of a
>> top-level transaction context manager with its own substransaction
>> method.
>
> Yeah, that would probably be the best option in this particular
> instance. Though I do still like the ability to have variables shadow
> each other, even if there's a way around one particular piece of code
> that uses the technique.
>

I have been following this sub-thread with interest, as it resonates with 
what I am doing in my project.

In my case, one update can trigger another, which can trigger another, etc. 
It is important that they are treated as a single transaction. Each object 
has its own 'save' method, so there is not one place where all updates are 
executed, and I found it tricky to control.

I came up with the following context manager -

class DbSession:
    """
    A context manager to handle database activity.
    """

    def __init__(self):
        self.conn = None
        self.no_connections = 0
        self.transaction_active = False

    def __enter__(self):
        if self.conn is None:
            self.conn = _get_connection()  # get connection from pool
            self.conn.cur = self.conn.cursor()
            # all updates in same transaction use same timestamp
            self.conn.timestamp = datetime.now()
        self.no_connections += 1
        return self.conn

    def __exit__(self, type, exc, tb):
        if type is not None:  # an exception occurred
            if self.transaction_active:
                self.conn.rollback()
                self.transaction_active = False
            self.conn.release()  # return connection to pool
            self.conn = None
            return  # will reraise exception
        self.no_connections -= 1
        if not self.no_connections:
            if self.transaction_active:
                self.conn.commit()
                self.transaction_active = False
            self.conn.cur.close()
            self.conn.release()  # return connection to pool
            self.conn = None

All objects created within a session share a common DbSession instance.

When any of them need any database access, whether for reading or for 
updating, they execute the following -

    with db_session as conn:
        conn.transaction_active = True  # this line must be added if 
updating
        conn.cur.execute(__whatever__)

Now it 'just works'. I don't have the need for save-points - either all 
updates happen, or none of them do.

Frank Millman


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