Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'method.': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'mind,': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; "'-'": 0.16; '*args):': 0.16; 'expend': 0.16; 'merely': 0.16; 'received:69.93': 0.16; 'run:': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'separate': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'this:': 0.26; 'updating': 0.26; 'second': 0.26; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'skip:@ 10': 0.30; "skip:' 10": 0.31; 'factor': 0.31; 'initialized': 0.31; 'class': 0.32; 'probably': 0.32; 'skip:c 30': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'could': 0.34; 'skip:- 50': 0.35; 'test': 0.35; 'but': 0.35; 'false': 0.36; 'yield': 0.36; 'charset:us- ascii': 0.36; "i'll": 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'access,': 0.60; 'received:173': 0.61; "you're": 0.61; 'first': 0.61; 'sample': 0.67; 'frank': 0.68; 'jul': 0.74; 'repeat': 0.74; 'object:': 0.84; 'reading,': 0.84; 'writing,': 0.84; '2013': 0.98 Date: Tue, 09 Jul 2013 09:07:58 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Default scope of variables 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> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:57049 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373387509 news.xs4all.nl 15927 [2001:888:2000:d::a6]:44852 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50260 On 07/09/2013 01:38 AM, Frank Millman wrote: > > "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. You could also do it like this: def updating(self): self.transaction_active = True return self and a sample object: class Tester(object): def __init__(self): self.transaction_active = False print 'initializied' def __enter__(self, *args): print '__enter__: transaction_active =', self.transaction_active return self def __exit__(self, *args): self.transaction_active = False print '__exit__: transaction_active =', self.transaction_active return def updating(self): self.transaction_active = True print 'updating: self.transaction_active =', self.transaction_active return self with Tester() as conn: print 'in first with block' print '-' * 50 with Tester().updating() as conn: print 'in second with block' with it's test run: ethan@hydra:~$ python test_cm.py initialized __enter__: transaction_active = False in first with block __exit__: transaction_active = False -------------------------------------------------- initialized updating: self.transaction_active = True __enter__: transaction_active = True in second with block __exit__: transaction_active = False -- ~Ethan~