Deutsch English Français Italiano |
<mailman.46.1725744071.2917.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!2.eu.feeder.erje.net!feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Karsten Hilbert <Karsten.Hilbert@gmx.net> Newsgroups: comp.lang.python Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks Date: Sat, 7 Sep 2024 23:20:57 +0200 Lines: 77 Sender: <karsten.hilbert@gmx.net> Message-ID: <mailman.46.1725744071.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> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de D19ZUoeT47OwkP9pnUuL+wbnvLDmoQbEiOXY939AqHdw== Cancel-Lock: sha1:2O0LyewjgytIKO+ocNYXWHHc0Io= sha256:GxBC6NK0hhyjdKh9AMareuOILDkMYGrDiVqOqUPHzEc= Return-Path: <karsten.hilbert@gmx.net> 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=gmx.net header.i=karsten.hilbert@gmx.net header.b=C5DT4Hdv; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'fairly': 0.05; 'received:212.227': 0.07; 'cc:addr:python-list': 0.09; 'gpg': 0.09; 'karsten': 0.09; 'post.': 0.09; 'schrieb': 0.09; 'log': 0.12; 'problem.': 0.15; 'that.': 0.15; '1713': 0.16; '2024': 0.16; 'boring': 0.16; 'choices.': 0.16; 'elsewhere': 0.16; 'ensures': 0.16; 'insufficient': 0.16; 'pythonic': 0.16; 'received:212.227.15': 0.16; 'subject:() ': 0.16; 'subject:proper': 0.16; 'subject:try': 0.16; 'problem': 0.16; 'reached': 0.17; 'cc:addr:python.org': 0.20; 'sat,': 0.22; 'code': 0.23; 'cc:2**1': 0.23; 'actual': 0.25; 'question': 0.32; 'sep': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34; 'best,': 0.38; 'quite': 0.39; 'exact': 0.40; 'something': 0.40; 'best': 0.61; 'sample': 0.61; "there's": 0.61; 'received:212': 0.62; 'involved': 0.63; 'full': 0.64; 'your': 0.64; 'eventually': 0.84; 'exceptions': 0.84; 'handled': 0.84; 'repetitive': 0.84; 'subject:within': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1725744058; x=1726348858; i=karsten.hilbert@gmx.net; bh=MLHhgDkyrfD78k1nLFaIpMlmvJLWwW2l1U4ZWrWfwxw=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=C5DT4HdvZGkCanhYkr8pkbTw5eX2nFMlBXdmOBmx/EPaGb0VRFgUZBloV8oOkCv+ gVagqhbFv/bKIded6ukjBmwy4VuEAx/IaS7CZnudVhUcio5jNLeeFZzv+kCFNXBLc gXH+JdiGasUXCPsGYZIQcNVbsz5g1tG+J6ffzyJbDMeSu4DwMujS1r883MpltIkcq JbAzNljg9xDnz0Ke+9GPMCIgvPlxtBRznVOmMnLpZNh8IvEVspUHbmHiXw7I3a6sr BzUz2MFFL8EypwgV+jxxEP+EtsdQpIEs7OUej4CIzdK/m1nZ9hOjyWXbHoLE0omcj m6b0X8EMsORvS+B73Q== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Content-Disposition: inline In-Reply-To: <4a1b12fc-24b7-4c7e-b1f2-6ec9c5f341c2@aklaver.com> Ma_X_il-Followup-to: d Re_X_turn-receipt-to: Karsten.Hilbert@gmx.net Di_X_sposition-Notification-To: Karsten.Hilbert@gmx.net X-Confi_X_rm-Reading-To: Karsten.Hilbert@gmx.net X-Pri_X_ority: 2 (High) X-Provags-ID: V03:K1:n+/1LV090OC2MIvwe+W7yO36UQhhUzp90RGdb3CH8TMjfUPHkYn xFl4+gbh0jDGEGPcgtd0AivJVkd434jmUoP7mhSkeIX4wCD3TD0bDvJTzYDptstsLjwZKF5 ZJMV6r2Yo2P5iFs/gp6DocHm9rlUbqwb1Oz7Wqml7fdCtMPKNRooSIeyFXCBM/knQRC9PSq sfd4SVeXYDBMR+mgoWF1w== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:COpfTIja9lQ=;+oZARKcMKelDTecYS72J0YtoEz9 yJ1uLUG79DhWR/svGWgtxRlCr69T7EKImcF4V4LfvzSdo6xYvZYg9sbLWIUrL+SSiK0+doVAT HwF8gmSUEYqKUwKjzG5/nkUotVL6YBwvvgiJK+NeUYI/Jn+zMotU+IzQMgWGoeZlTY33SLkGl wL+w41ePmvEXekjmEgaY0bWb8w7NDvuaN3NBil88HZU1IYZy9wWTBlzolWuJLXIOOWgET6V97 ljBGf+hKGe+X+cc5S6XirESKyGEOU7JnhW3MbnTfypVOVsOkcWl9Lz58nvMzssuBo2QEUoHXJ Bt1cPtJByVkPh/EJ6NtKrrcItKCtBVuJLtN95SzohsthUqxGqBEtzE8e1quQDzh8h45MjYFCj MR2olkOzWwkYnYt6WV3HFCmk8fJnEAI87CL/8lx5yiQPmylvU0AdqnjzPID4aFylxDB9svBY6 GdvCdwftXLDDVFNs7pWGYX0aDwxa2Gp+bB+/ssoc5varwgOmW3V391J5IylinDayQxdsgsGlT tc05VbxtnwJDXBn1FA+QMM3iZtdlMP7rgAVPSH28MFko3fjMRRzaSsDNwtytXjPRFBnsXIR/8 aNJl2TKaufYBsnXIDZTts+UoetuQdRWHvy/+X/oNFTXg6NMwHX23vwUOUulVRBYc9XVl85/gy JsSwIC8i14LJpnqbH6IqDGvSySH16CVn5jtVKaQfUnxYYU79jvnM3TT8E482EfkT4kYBEbkNv 6Ch9tOAZV7u2dgMrIlcby38qw86FEgPh45UYyg5r/ttV/YQiB2w8bl0Q7/DjnRl2t83nU4yV9 bMATcUnT6f+teUKVaFl9Wd8w== 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: <ZtzDubguRDWXwV-t@hermes.hilbert.loc> 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> Bytes: 7311 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 d= ifferent 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 =2D- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B