Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'subject:Python': 0.06; "'',": 0.07; 'element': 0.07; 'ignored': 0.07; 'json': 0.07; 'nested': 0.07; 'subject:Question': 0.07; 'transform': 0.07; 'string': 0.09; '===': 0.09; 'pgp': 0.09; 'preferable': 0.09; 'skip:o 60': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'variant': 0.09; 'cc:addr:python-list': 0.11; 'martin': 0.11; 'python': 0.11; '-----begin': 0.16; '-----end': 0.16; 'c_int': 0.16; 'coerced': 0.16; 'fine.': 0.16; 'gnupg': 0.16; 'hash:': 0.16; 'nesting': 0.16; 'personally,': 0.16; 'practice?': 0.16; 'sha1': 0.16; 'skip:/ 60': 0.16; 'skip:0 60': 0.16; 'skip:u 60': 0.16; 'ignore': 0.16; 'language': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'message-----': 0.19; 'example': 0.22; 'cc:addr:python.org': 0.22; 'integer': 0.24; 'necessary.': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; 'options': 0.25; 'pass': 0.26; 'signed': 0.27; 'values': 0.27; 'function': 0.29; "doesn't": 0.30; 'url:mailman': 0.30; 'code': 0.31; '>>>>': 0.31; 'exceptions': 0.31; 'skip:i 60': 0.31; 'skip:k 60': 0.31; 'this.': 0.32; 'another': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'except': 0.35; 'something': 0.35; 'add': 0.35; 'there': 0.35; 'options:': 0.36; 'version:': 0.36; 'url:listinfo': 0.36; 'charset :us-ascii': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'e.g.': 0.38; 'does': 0.39; 'url:mail': 0.40; 'catch': 0.60; 'first': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'incoming': 0.72; 'rafael': 0.84; 'received:as9105.com': 0.84; 'received:dsl.as9105.com': 0.84; 'received:172.17': 0.93 X-Env-Sender: martin.kemp@ingg.com X-Msg-Ref: server-7.tower-206.messagelabs.com!1414586623!12496733!1 X-Originating-IP: [80.45.145.137] X-StarScan-Received: X-StarScan-Version: 6.12.3; banners=ingg.com,-,- X-VirusChecked: Checked From: Martin Kemp To: Rafael Romero Carmona Subject: Re: Python Style Question Thread-Topic: Python Style Question Thread-Index: AQHP82WWO+xTm/fqrEiFy9J0pDCw0A== Date: Wed, 29 Oct 2014 12:42:56 +0000 References: <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.23.126.199] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 29 Oct 2014 12:43:39.0652 (UTC) FILETIME=[F6ED8C40:01CFF375] X-Mailman-Approved-At: Wed, 29 Oct 2014 14:37:45 +0100 Cc: "python-list@python.org" 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414589867 news.xs4all.nl 2899 [2001:888:2000:d::a6]:42953 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79911 -----BEGIN PGP SIGNED MESSAGE-----=0A= Hash: SHA1=0A= =0A= On 29/10/2014 12:01, Rafael Romero Carmona wrote:=0A= > 2014-10-29 12:25 GMT+01:00 Martin Kemp : On=0A= > 29/10/2014 10:45, Anton wrote:=0A= >>>> Let's say I have an incoming list of values *l*. Every=0A= >>>> element of *l* can be one of the following options: 1) an=0A= >>>> integer value 2) a string in form of '', e.g. '7'=0A= >>>> 3) a string with a json serialization of an integer value,=0A= >>>> e.g. '"7"' 4) something else that should be ignored=0A= >>>> =0A= >>>> I need to transform this list into another list with values=0A= >>>> from options 1)-3) coerced to int. The code below should do=0A= >>>> this.=0A= >>>> =0A= >>>> =0A= >>>> Variant 1 =3D=3D=3D=0A= >>>> =0A= >>>> values =3D [] for c in l: # Case 1) or 2) try: c_int =3D int(c)=0A= >>>> except ValueError: pass else: values.add(c_int) continue=0A= >>>> =0A= >>>> # Case 3) try: c_int =3D int(json.loads(c)) except ValueError:=0A= >>>> pass else: values.add(c_int) continue=0A= >>>> =0A= >>>> =3D=3D=3D=0A= >>>> =0A= >>>> Is this code ugly? Does it follow EAFP? Am I missing=0A= >>>> something in language best practice?=0A= >>>> =0A= >>>> Or maybe below is more preferable way with a nested=0A= >>>> try...except clause?=0A= >>>> =0A= >>>> Variant 2 =3D=3D=3D values =3D [] for c in l: # Case 1) or 2) try:=0A= >>>> c_int =3D int(c) except ValueError: # Case 3) try: c_int =3D =0A= >>>> int(json.loads(c)) except ValueError: pass else:=0A= >>>> values.add(c_int) continue else: values.add(c_int) continue=0A= >>>> =3D=3D=3D=0A= >>>> =0A= >>>> Thanks, Anton.=0A= >>>> =0A= >>>> =0A= > =0A= > Your first example is perfectly fine and is EAFP=0A= > =0A= > =0A= >> Actually it doesn't work because there is no add function and it =0A= >> doesn't catch the TypeError function to ignore other exceptions=0A= >> than ValueError. Doesn't it? I tested in Python 2.7.6 and 3.4.=0A= > =0A= > Personally, I prefer to avoid nesting when it's not necessary.=0A= > =0A= >> -- https://mail.python.org/mailman/listinfo/python-list=0A= =0A= Ah ok, style-wise it was fine.=0A= =0A= - -- =0A= Martin Kemp (martin.kemp@ingg.com)=0A= =0A= -----BEGIN PGP SIGNATURE-----=0A= Version: GnuPG v2=0A= =0A= iQEcBAEBAgAGBQJUUOD+AAoJEJ0Re0UIDzSu1KQIAK6aCMOv4VqOjmm/zoQrmzLf=0A= UGBCLwtHrnDkbXFAIweTSFiM1uf9TDaRpJqY1IrPbJHI4/EAP0Hu07nyx3V6HgzM=0A= /+Wb3DkpjW+JQoVqDSGzE/dTPJcU3/b1/EWWpbu72JHplqz9laEAFt9muWyDPs9u=0A= kDgM06mDd50lsi83W3i0H1iGL6YbLtsik+/x4G4mMjdq1o9BvRpUjkIiOx7yJ/BR=0A= OYzdudltXGqlXcToufHTU2lUv2C0RoHHNO4kytiLoUekCBdGE+Jy/6gQq/AKQu4G=0A= 0RYjCOnKNgugfdmDuHi0julPtTEzc+MdY/CcPob4cyy8RDzfQGklGKHP7f9+SJs=3D=0A= =3DhjWU=0A= -----END PGP SIGNATURE-----=0A=