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.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'method.': 0.07; 'mind,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'wrote': 0.14; 'argument,': 0.16; 'attribute.': 0.16; 'expend': 0.16; 'factory': 0.16; 'merely': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.18; 'separate': 0.22; 'updating': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'skip:@ 10': 0.30; 'factor': 0.31; 'probably': 0.32; 'skip:c 30': 0.32; 'received:co.za': 0.34; 'received:za': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'but': 0.35; 'yield': 0.36; "i'll": 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'access,': 0.60; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'frank': 0.68; 'received:41': 0.70; 'jul': 0.74; 'repeat': 0.74; 'reading,': 0.84; 'writing,': 0.84; '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:38:21 +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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373359106 news.xs4all.nl 15882 [2001:888:2000:d::a6]:50020 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50226 "Ian Kelly" wrote in message news:CALwzid=FzgjPebifx1stDBkh8iwLtWggwwPTPhZ1ykYg+05wEg@mail.gmail.com... > On Tue, Jul 9, 2013 at 1:35 AM, Frank Millman wrote: >> 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__) > > I'd probably factor out the transaction_active line into a separate > DbSession method. > > @contextmanager > def updating(self): > with self as conn: > conn.transaction_active = True > yield conn > > Then you can do "with db_session" if you're merely reading, or "with > db_session.updating()" if you're writing, and you don't need to repeat > the transaction_active line all over the place. > I'll bear it in mind, but I will have to expend some mental energy to understand it first , so it will have to wait until I can find some time. > I would also probably make db_session a factory function instead of a > global. It is not actually a global. When I create a new session, I create a db_session instance and store it as a session attribute. Whenever I create a database object during the session, I pass in the instance as an argument, so they all share the same one. Frank