Deutsch English Français Italiano |
<mailman.49.1725803955.2917.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Rob Cliffe <rob.cliffe@btinternet.com> Newsgroups: comp.lang.python Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks Date: Sun, 8 Sep 2024 14:58:03 +0100 Lines: 99 Message-ID: <mailman.49.1725803955.2917.python-list@python.org> References: <Ztx1sZMGTnA3eLP1@hermes.hilbert.loc> <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com> <ZtytJMhyvtExPxfF@hermes.hilbert.loc> <5ee80b84-f04b-454d-ab39-45572e0751a1@aklaver.com> <Zty7ZCMwKQl4C4Id@hermes.hilbert.loc> <4a1b12fc-24b7-4c7e-b1f2-6ec9c5f341c2@aklaver.com> <ZtzDubguRDWXwV-t@hermes.hilbert.loc> <62133ebd-a4a3-471b-9acc-3a988b4fcbd7@btinternet.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de pAcKru9IO4WoPwxH1MO+Kwmn4EiDt1OTvmJlZghDSpLg== Cancel-Lock: sha1:eeKJ6aPUkgOEV1CWbJqb/GMJnCI= sha256:hehNcCIswUOVBtbgIVe+Jdve+ffz9rso2d6RF1K1ZcA= Return-Path: <rob.cliffe@btinternet.com> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=btinternet.com header.i=@btinternet.com header.b=pC9aXZkV; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'fairly': 0.05; 'thing.': 0.07; 'wrong.': 0.07; 'gpg': 0.09; 'karsten': 0.09; 'post.': 0.09; 'schrieb': 0.09; 'something,': 0.09; 'log': 0.12; 'problem.': 0.15; 'that.': 0.15; '(it': 0.16; '1713': 0.16; '2024': 0.16; 'be)': 0.16; 'boring': 0.16; 'choices.': 0.16; 'elsewhere': 0.16; 'ensures': 0.16; 'executed': 0.16; 'hilbert': 0.16; 'indentation': 0.16; 'insufficient': 0.16; 'pythonic': 0.16; 'subject:() ': 0.16; 'subject:proper': 0.16; 'subject:try': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'reached': 0.17; 'to:addr:python-list': 0.20; 'sat,': 0.22; 'version': 0.23; 'code': 0.23; 'actual': 0.25; 'seems': 0.26; 'bit': 0.27; '>>>': 0.28; 'suggest': 0.28; 'header :User-Agent:1': 0.30; 'whole': 0.30; 'question': 0.32; 'python- list': 0.32; 'right,': 0.32; 'sep': 0.32; 'unless': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; "i'm": 0.33; 'header:In- Reply-To:1': 0.34; 'missing': 0.37; 'received:192.168': 0.37; 'best,': 0.38; 'put': 0.38; 'quite': 0.39; 'block': 0.39; 'exact': 0.40; 'wishes': 0.40; 'something': 0.40; 'try': 0.40; 'best': 0.61; 'sample': 0.61; "there's": 0.61; 'involved': 0.63; 'feel': 0.63; 'full': 0.64; 'your': 0.64; 'less': 0.65; 'right': 0.68; 'and,': 0.69; '1st': 0.78; '3rd': 0.81; 'extra': 0.84; 'eventually': 0.84; 'exceptions': 0.84; 'handled': 0.84; 'repetitive': 0.84; 'rob': 0.84; 'subject:within': 0.84; 'replacing': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com; s=btmx2012311; t=1725803953; bh=8r3lDpRWrWMKdagYeOlsRBhtz+CdzgwqSDTnfPEhcv8=; h=Message-ID:Date:MIME-Version:Subject:To:References:From:In-Reply-To; b=pC9aXZkV2/wauEk7KEklwlCmDT9ZwCAy3QcCrY0HJl+tkyRNf+lZepMQUX9CgmmI8+DBv4c6o3R8QinXej7YEmKxuAQdn2KPS5wn7xa+bkj1Lf8LBXb9jUWyVmrN1EUhCzldovXrom/9hicMtUjSJvpZnAXewxNpnCfh224ujfdv7o3BIYAXRGO1Nt6OzkwrqiiPDZ0qV9t+HZZjXRllk1GU7RylheU25w3H+7ukKtD4ddB6XJqOIf1N2CmLaZLtokuEBz2Ou0Dcu+KqDd8ZTOp2AIcDowyIxQ+1dRt2qbK+6smCWb8OFDdujb/2CRMTZAKh30RMqvEKkXpGtVwbqw== X-SNCR-Rigid: 669441800680192F X-Originating-IP: [86.175.96.244] X-OWM-Source-IP: 86.175.96.244 X-OWM-Env-Sender: rob.cliffe@btinternet.com X-VadeSecure-score: verdict=clean score=0/300, class=clean X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgeeftddrudeihedgjeduucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuueftkffvkffujffvgffngfevqffopdfqfgfvnecuuegrihhlohhuthemuceftddunecunecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptfhosgcuvehlihhffhgvuceorhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomheqnecuggftrfgrthhtvghrnhepteetueffteffgeetffehveehjeduffekleetudevkeetkeehteevudejgeekheevnecukfhppeekiedrudejhedrleeirddvgeegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghloheplgduledvrdduieekrddurddvtdekngdpihhnvghtpeekiedrudejhedrleeirddvgeegpdhmrghilhhfrhhomheprhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomhdpnhgspghrtghpthhtohepuddprhgtphhtthhopehphihthhhonhdqlhhishhtsehphihthhhonhdrohhrghdprhgvvhfkrfephhhoshhtkeeiqddujeehqdeliedqvdeggedrrhgrnhhgvgekiedqudejhedrsghttggvnhhtrhgrlhhplhhushdrtghomhdprghuthhhpghushgvrheprhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomhdpghgvohfkrfepifeupdfovfetjfhoshhtpegsthhprhgurhhgohdttdei X-RazorGate-Vade-Verdict: clean 0 X-RazorGate-Vade-Classification: clean X-SNCR-hdrdom: btinternet.com X-SNCR-hdrdom: btinternet.com User-Agent: Mozilla Thunderbird Content-Language: en-GB In-Reply-To: <ZtzDubguRDWXwV-t@hermes.hilbert.loc> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: General discussion list for the Python programming language <python-list.python.org> List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> List-Archive: <https://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: <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> X-Mailman-Original-Message-ID: <62133ebd-a4a3-471b-9acc-3a988b4fcbd7@btinternet.com> X-Mailman-Original-References: <Ztx1sZMGTnA3eLP1@hermes.hilbert.loc> <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com> <ZtytJMhyvtExPxfF@hermes.hilbert.loc> <5ee80b84-f04b-454d-ab39-45572e0751a1@aklaver.com> <Zty7ZCMwKQl4C4Id@hermes.hilbert.loc> <4a1b12fc-24b7-4c7e-b1f2-6ec9c5f341c2@aklaver.com> <ZtzDubguRDWXwV-t@hermes.hilbert.loc> Bytes: 8328 On 07/09/2024 22:20, Karsten Hilbert via Python-list wrote: > Am Sat, Sep 07, 2024 at 02:09:28PM -0700 schrieb Adrian Klaver: > >>> Right, and this was suggested elsewhere ;) >>> >>> And, yeah, the actual code is much more involved :-D >>> >> I see that. >> >> The question is does the full code you show fail? >> >> The code sample you show in your original post is doing something very different then >> what you show in your latest post. At this point I do not understand the exact problem >> we are dealing with. > We are not dealing with an unsolved problem. I had been > asking for advice where to best place that .commit() call in > case I am overlooking benefits and drawbacks of choices. > > The > > try: > do something > except: > log something > finally: > .commit() > > cadence is fairly Pythonic and elegant in that it ensures the > the .commit() will always be reached regardless of exceptions > being thrown or not and them being handled or not. > > It is also insufficient because the .commit() itself may > elicit exceptions (from the database). > > So there's choices: > > Ugly: > > try: > do something > except: > log something > finally: > try: > .commit() > except: > log some more > > Fair but not feeling quite safe: > > try: > do something > .commit() > except: > log something > > Boring and repetitive and safe(r): > > try: > do something > except: > log something > try: > .commit() > except: > log something > > I eventually opted for the last version, except for factoring > out the second try: except: block. > > Best, > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B Unless I'm missing something, the 1st & 3rd versions always do the commit() even after the first bit fails, which seems wrong. I suggest the 1st version but replacing "finally" by "else". Then the try-commit-except will not be executed if the "something" fails. Perhaps the extra indentation of the second try block is a bit ugly, but it is more important that it does the right thing. If it is convenient (it may not be) to put the whole thing in a function, you may feel that the follwing is less ugly: try: do something except: log something return try: .commit() except: log some more return Best wishes Rob Cliffe