Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79907

Re: Python Style Question

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <martin.kemp@ingg.com>
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; 'try:': 0.09; 'valueerror:': 0.09; 'variant': 0.09; 'martin': 0.11; '-----begin': 0.16; '-----end': 0.16; 'c_int': 0.16; 'coerced': 0.16; 'gnupg': 0.16; 'hash:': 0.16; 'nesting': 0.16; 'personally,': 0.16; 'practice?': 0.16; 'sha1': 0.16; 'skip:j 60': 0.16; 'language': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'message-----': 0.19; 'example': 0.22; 'to:name :python-list@python.org': 0.22; 'integer': 0.24; 'necessary.': 0.24; 'fine': 0.24; 'options': 0.25; 'pass': 0.26; 'signed': 0.27; 'values': 0.27; 'code': 0.31; 'skip:i 60': 0.31; 'this.': 0.32; 'another': 0.32; 'maybe': 0.34; 'except': 0.35; 'something': 0.35; 'options:': 0.36; 'skip:s 60': 0.36; 'version:': 0.36; 'charset :us-ascii': 0.36; 'should': 0.36; 'list': 0.37; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'more': 0.64; 'incoming': 0.72; 'received:as9105.com': 0.84; 'received:dsl.as9105.com': 0.84; 'skip:9 60': 0.91; 'received:172.17': 0.93
X-Env-Sender martin.kemp@ingg.com
X-Msg-Ref server-13.tower-27.messagelabs.com!1414582004!13185322!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 <martin.kemp@ingg.com>
To "python-list@python.org" <python-list@python.org>
Subject Re: Python Style Question
Thread-Topic Python Style Question
Thread-Index AQHP82WWO+xTm/fqrEiFy9J0pDCw0A==
Date Wed, 29 Oct 2014 11:25:57 +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 11:26:40.0855 (UTC) FILETIME=[35E90E70:01CFF36B]
X-Mailman-Approved-At Wed, 29 Oct 2014 12:43:02 +0100
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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 <http://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>
Newsgroups comp.lang.python
Message-ID <mailman.15294.1414582983.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414582983 news.xs4all.nl 2860 [2001:888:2000:d::a6]:50427
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79907

Show key headers only | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 29/10/2014 10:45, Anton wrote:
> Let's say I have an incoming list of values *l*. Every element of
> *l* can be one of the following options: 1) an integer value 2) a
> string in form of '<int_value>', e.g. '7' 3) a string with a json
> serialization of an integer value, e.g. '"7"' 4) something else
> that should be ignored
> 
> I need to transform this list into another list with values from
> options 1)-3) coerced to int. The code below should do this.
> 
> 
> Variant 1 ===
> 
> values = [] for c in l: # Case 1) or 2) try: c_int = int(c) except
> ValueError: pass else: values.add(c_int) continue
> 
> # Case 3) try: c_int = int(json.loads(c)) except ValueError: pass 
> else: values.add(c_int) continue
> 
> ===
> 
> Is this code ugly? Does it follow EAFP? Am I missing something in
> language best practice?
> 
> Or maybe below is more preferable way with a nested try...except
> clause?
> 
> Variant 2 === values = [] for c in l: # Case 1) or 2) try: c_int =
> int(c) except ValueError:  # Case 3) try: c_int =
> int(json.loads(c)) except ValueError: pass else: values.add(c_int) 
> continue  else: values.add(c_int) continue ===
> 
> Thanks, Anton.
> 
> 

Your first example is perfectly fine and is EAFP

Personally, I prefer to avoid nesting when it's not necessary.

- -- 
Martin Kemp (martin.kemp@ingg.com)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJUUM7zAAoJEJ0Re0UIDzSucugIALn/zY8RdpP8iaMShHoszzqf
I0zl0mFHyqhNtwgQ0ZF7VGO+H+U0Dk8rhzTYOmEMzPTKNBGwll3fda9mOnrK9Xvp
9gQjII6DyQIWH7Z3dLcLr2e1j8OMNUSL6UmAYs8urNSIKZLowdV3JI4G/bLyW0KS
y5Ko8dI6y5nOJ1P9XCmPTmags43UZfR8DrBUaAbzNcS8FGwmUE2KBkEhLQOvmpJi
jmMc7wMOpq0jL+XbA+7pHUqoVZ7w1tUFjuy9I3h45tgPuTFAFB0gX+FpE+oVgO5o
spQpVaOPEYN9ceLgHdKSxzdVIhOQLE6H/SYNHlsEW/ZNM6aR9n4yipgkOmtJ0+M=
=WzHA
-----END PGP SIGNATURE-----

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 03:42 -0700
  Re: Python Style Question Martin Kemp <martin.kemp@ingg.com> - 2014-10-29 11:25 +0000
  Re: Python Style Question Rafael Romero Carmona <rafaelromcar@gmail.com> - 2014-10-29 12:09 +0100
    Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 12:09 -0700
  Re: Python Style Question Rafael Romero Carmona <rafaelromcar@gmail.com> - 2014-10-29 12:52 +0100
    Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 12:23 -0700
  Re: Python Style Question Martin Kemp <martin.kemp@ingg.com> - 2014-10-29 12:42 +0000
  Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-30 22:10 +1100
    Re: Python Style Question MRAB <python@mrabarnett.plus.com> - 2014-10-30 12:21 +0000
      Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-31 09:48 +1100
        Re: Python Style Question Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-31 04:55 +0000
    Re: Python Style Question Roy Smith <roy@panix.com> - 2014-10-30 20:34 -0400
      Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-31 14:52 +1100
    Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-11-12 12:31 -0800

csiph-web