Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.07; 'explicit': 0.07; 'nested': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type,': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; "(it's": 0.16; 'command,': 0.16; 'fyi,': 0.16; 'hmm.': 0.16; 'inclined': 0.16; 'interest,': 0.16; 'postgresql).': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'throw': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'library': 0.18; 'commit': 0.19; 'server,': 0.19; 'command': 0.22; 'saying': 0.22; 'header:X-Complaints-To:1': 0.27; '(which': 0.31; 'code': 0.31; "skip:' 10": 0.31; 'class': 0.32; 'open': 0.33; 'skip:_ 10': 0.34; 'received:co.za': 0.34; 'received:za': 0.34; "i'd": 0.34; 'possible.': 0.35; 'transaction': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'c++': 0.36; 'false': 0.36; 'doing': 0.36; 'possible': 0.36; 'should': 0.36; 'manager': 0.38; 'generic': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'release': 0.40; 'complete': 0.62; 'frank': 0.68; 'received:41': 0.70; 'connection.': 0.74; 'jul': 0.74; 'this;': 0.91; 'connection,': 0.95; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Default scope of variables Date: Tue, 9 Jul 2013 10:22:48 +0200 References: <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com><51d508ed$0$6512$c3e8da3$5496439d@news.astraweb.com><51d5a504$0$29999$c3e8da3$5496439d@news.astraweb.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373358173 news.xs4all.nl 15956 [2001:888:2000:d::a6]:41348 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50224 "Chris Angelico" wrote in message news:CAPTjJmr4Mr0qCGWqXwyvDCZ55NuaV79VbTT8BJNDSDvhrKq15w@mail.gmail.com... > On Tue, Jul 9, 2013 at 5:35 PM, Frank Millman wrote: >> I have been following this sub-thread with interest, as it resonates with >> what I am doing in my project. > > Just FYI, none of my own code will help you as it's all using libpqxx, > but the docs for the library itself are around if you want them (it's > one of the standard ways for C++ programs to use PostgreSQL). > I support multiple databases (PostgreSQL, MS SQL Server, sqlite3 at this stage) so I use generic Python as much as possible. >> I came up with the following context manager - >> >> class DbSession: >> def __exit__(self, type, exc, tb): >> if self.transaction_active: >> self.conn.commit() >> self.transaction_active = False > > Hmm. So you automatically commit. I'd actually be inclined to _not_ do > this; make it really explicit in your code that you now will commit > this transaction (which might throw an exception if you have open > subtransactions). > I endeavour to keep all my database activity to the shortest time possible - get a connection, execute a command, release the connection. So setting 'transaction_active = True' is my way of saying 'execute this command and commit it straight away'. That is explicit enough for me. If there are nested updates they all follow the same philosophy, so the transaction should complete quickly. Frank