Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #198: Post-it Note Sludge leaked into the monitor.


comp / comp.lang.python / Re: Best use of "open" context manager

SubjectAuthor
* Re: Best use of "open" context managerAlbert-Jan Roskam
`- Re: Best use of "open" context managerStefan Ram

1
Subject: Re: Best use of "open" context manager
From: Albert-Jan Roskam
Newsgroups: comp.lang.python
Date: Fri, 12 Jul 2024 09:30 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: sjeik_appie@hotmail.com (Albert-Jan Roskam)
Newsgroups: comp.lang.python
Subject: Re: Best use of "open" context manager
Date: Fri, 12 Jul 2024 11:30:14 +0200
Lines: 11
Message-ID: <mailman.33.1720776627.2981.python-list@python.org>
References: <ZotSwXK2ajuqmWHq@cskk.homeip.net>
<DB9PR10MB66894E047D279633FA7E7B9883A62@DB9PR10MB6689.EURPRD10.PROD.OUTLOOK.COM>
Mime-Version: 1.0
Content-Type: text/plain; charset="utf-8"
X-Trace: news.uni-berlin.de ZxtW9CaAe8zNdGWCzdEboArOPKoSxJbGR//6OBBX8cRA==
Cancel-Lock: sha1:Ba5P+kyzVezadONkwNBRDJL01GM= sha256:Sx3OmSe/PE8MfvQv5AryFOJUR10R3Em/7eDm36I3WLE=
Return-Path: <sjeik_appie@hotmail.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=hotmail.com header.i=@hotmail.com header.b=u1Vq+JSu;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.103
X-Spam-Level: *
X-Spam-Evidence: '*H*': 0.87; '*S*': 0.07; 'this:': 0.03; 'cc:addr
:python-list': 0.09; 'cc:name:python': 0.16; 'indentation': 0.16;
'none:': 0.16; 'pylint': 0.16; 'cc:addr:python.org': 0.20; 'way.':
0.22; 'cc:2**1': 0.23; 'header:In-Reply-To:1': 0.34; 'using':
0.37; 'skip:o 10': 0.61; 'less': 0.65; 'records': 0.75;
'received:40.92.90': 0.84; 'subject:manager': 0.84;
'subject:open': 0.84
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=gFVviPsOlFcHw+I/mTu1BtHdSLCcLxpH4UgAwTGkwhNwqS4SCkYsooNA1N38JYi6O7x0/F0uSIZTy2NtxOVu/LpQ2CJjssJdAkCkctLhsUtxoxkEYt1q8xxKdt8K63euJA+G/23+M2jkPWu6iJzt0TWwY9xxC1GZc8FwqyleSF9azxelRum+ePV2MvZB8e/r0Wn05NabDzPNjIBDfKnRomYxuZ3OlefQYhhP4JangqxmkxAERavI0rIF7pL/Hu4es3OVCsbArmvPc8k92FXKdhXCQOGumwzRLuP57hlKE+mLTJdgKpnTpfmc0Cwg6Qp5wGGZJD0ioa2r5TGQq680/w==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector10001;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=gGoNhwvn+wMqZ8Fw2HVBzjkzg8JrDTLEH+qq8GHcWvg=;
b=Hi19IZnm6g2qKcdV35OxTXbMMjMj2QgKH4d6SbLIDJje0CEFDxjFNzp+/7IDphRNapTqgGOJzNOQQRicJY5AfNeTRC7efxQFZIGVxXJhwR97rQhtCJv1ZWUvB5JEQGpTPIH0fOVHkXdrtbh63gMZ5PMnUN2a82K7S7b6p9J+ZVvJCGFPRbqNL+/9r+aEpTNmxnVHfRVwhNlk8e3HEg0DlKaFmEfqOgvm1cHrFRryyHPghK9HID37/N33SanTiM118/ycVsmrf8nmpKEV9vfCnO6nxxob2gPKgJamW2mPAEIhCB/P5/qOkMrLqvFPxpRGhxYWHqONNqM/yZFotgVgNQ==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none;
dkim=none; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com;
s=selector1;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=gGoNhwvn+wMqZ8Fw2HVBzjkzg8JrDTLEH+qq8GHcWvg=;
b=u1Vq+JSuY5FDRCft+SSTyK1nubJlHChNI1kU2JP5M/mtXJAUSAKR5YMKZVHXfpWhr26R3NyDokvsCzGqamC9G80NcKR1uOoONUiFsSSMyeC4aosFnngh90BuJH196adX2qCIp6tUfu5vL973IysOlwZdfOLm8yFlvNWQpcixCZ6jUMH751qWtQYVADwVwE0/en0HiD3ccAf9t5QtAc8OBk1sGU4oj0EuqG65Upah/Y1ODC1OsmoRRNxWXwXIWQ/6VxedUkcUB2/a7JjmyjhxZOlQljDjErDMaVOwTk13WnRCEm9OnFMU9NWa7Qg0fiqtGCSl845oHSWQQ8QmuRfsGA==
X-Android-Message-ID: <2fed352f-36b0-46c7-926b-d5067c0d43da@email.android.com>
In-Reply-To: <ZotSwXK2ajuqmWHq@cskk.homeip.net>
X-TMN: [VRSUORgBLi9E4hDkmPUnkAM0aM8DOAFc]
X-ClientProxiedBy: AM8P190CA0029.EURP190.PROD.OUTLOOK.COM
(2603:10a6:20b:219::34) To DB9PR10MB6689.EURPRD10.PROD.OUTLOOK.COM
(2603:10a6:10:3d3::21)
X-Microsoft-Original-Message-ID: <2fed352f-36b0-46c7-926b-d5067c0d43da@email.android.com>
X-MS-Exchange-MessageSentRepresentingType: 1
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: DB9PR10MB6689:EE_|DU0PR10MB5632:EE_
X-MS-Office365-Filtering-Correlation-Id: fde9d3a2-38f6-4db2-795f-08dca2553eba
X-Microsoft-Antispam: BCL:0;
ARA:14566002|19110799003|8060799006|461199028|3412199025|440099028;
X-Microsoft-Antispam-Message-Info: A1co3wGYOYGDeGgPSmGYP1JPQW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n5G/ckt
r8Xn26y9u8L1ABpfwNZFkNOySy/iq7YBOh1zm0FVESCTA
svq5cDxrKavhwbqDSFEUpQKcuFLaZ6lb0sCH7jBC6Im2f
n5laPnE2fTff5jPnWMNEFullPYEE7jUX2OeVI5R314CSp
Kputp8FWyQZH9EeXyMaQ4wCObsjdPkG8rIN3A/qKlLx1x
0RwYAOiM/xbom4jA9ps/XdprAyizGuKivYgjZcW3j0+Og
YOUMyTKua0si2r0sutsZxqM8IMX70Pr03ieGzDLgaZuw6
+05pxmcXf6K7mLPm/iOrSkET+IIAHyjiP85D0JrH7/2kn
tmSghe2LbY6HC9EDGXuz0X81f0NDfuWIOa2lTJ3P68K3S
zY2ZMhrMc/nKw/D7V7I9+nvkD8zSXrhCckuiEV0jBoUEY
p2LjDuGF1DRjeuBC16XucVEhODJJXr3nsQFopAREu5mmr
HQJNyCzcdWbPTF5Z28CcVpXqvg8bl0fxs2saCvaF7Jgnl
VWneTtXbM6fiWsOkjRlIcgXO6CwNG49Cr7DDrLHyAwIOT
zB28B6SBmOkKGbu1PFlGdrmcm+Q+S7Uz03CXMNJgS2vUX
Vg6hngXigcW3VBdA/Iao5ic0hI2ICjfMMobrRuKoBgY90
+ygHuNcfmwjKgDNgK/FsZLzj/2NpYad6IyJh0aaD99euq
KsiPx4EiNLWTk7ynj7UYjXdRNXUKf843QvlZHgHp0feJv
M27f5LJBc0KqtO9+vJYIO+3uG0FPfFgHSeUskajQY9e+Z
r/k7TWYSyq04GVKATMs+bdMBoRfrRGRSURvJg9DQ60jmQ
5w3lztc58LZ94lol4P5cwPnhBgr7qY7akTD9nh6TB2KMm
mmzEQNeh9bHTYxcg50gFA3X3uJQ4hwfrOVKM+mp3TnpY=
X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-4359a.templateTenant
X-MS-Exchange-CrossTenant-Network-Message-Id: fde9d3a2-38f6-4db2-795f-08dca2553eba
X-MS-Exchange-CrossTenant-AuthSource: DB9PR10MB6689.EURPRD10.PROD.OUTLOOK.COM
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jul 2024 09:30:19.2233 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa
X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR10MB5632
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <DB9PR10MB66894E047D279633FA7E7B9883A62@DB9PR10MB6689.EURPRD10.PROD.OUTLOOK.COM>
View all headers

Or like below, although pylint complains about this: "consider using
with". Less indentation this way.
f = None
try:
f = open(FILENAME)
records = f.readlines()
except Exception:
sys.exit(1)
finally:
if f is not None:
f.close()

Subject: Re: Best use of "open" context manager
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Fri, 12 Jul 2024 11:55 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Best use of "open" context manager
Date: 12 Jul 2024 11:55:13 GMT
Organization: Stefan Ram
Lines: 17
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <code-20240712125444@ram.dialup.fu-berlin.de>
References: <ZotSwXK2ajuqmWHq@cskk.homeip.net> <DB9PR10MB66894E047D279633FA7E7B9883A62@DB9PR10MB6689.EURPRD10.PROD.OUTLOOK.COM> <mailman.33.1720776627.2981.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 4yPAk+/uO/EvWB3cwib2XwDkv1fd44UJY8H3rwTr53+d8A
Cancel-Lock: sha1:E5E+6zarKdjcQ1uWVhWgJsGTxV0= sha256:B/QTMtW+rT2etISOxUbTh9zCZGFh5T9wVR8xdQiPoxo=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: de-DE-1901, en-US
View all headers

Albert-Jan Roskam <sjeik_appie@hotmail.com> schrieb oder zitierte:
>Or like below, although pylint complains about this: "consider using
>with". Less indentation this way.

>f = None
>try:
> f = open(FILENAME)
> records = f.readlines()

This try clause would also catch exception raised by the readlines
call, and I thought that this was what the OP was looking to avoid.

>except Exception:
> sys.exit(1)

When this code is part of a library, it might not be appropriate
to make the whole application exit at this point.

1

rocksolid light 0.9.8
clearnet tor