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


Groups > comp.lang.python > #79908

Re: Python Style Question

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rafaelromcar@gmail.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; 'tries': 0.07; 'string': 0.09; '===': 0.09; '[1,': 0.09; 'preferable': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'variant': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; "'0',": 0.16; '(1,': 0.16; '-1,': 0.16; '11:42': 0.16; '2)]': 0.16; '==========': 0.16; 'c_int': 0.16; 'coerced': 0.16; 'integer,': 0.16; 'integer.': 0.16; 'practice?': 0.16; 'typeerror:': 0.16; 'valueerror': 0.16; 'exception': 0.16; 'language': 0.16; 'thanks,': 0.17; 'import': 0.22; 'cc:addr:python.org': 0.22; 'integer': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'options': 0.25; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'exceptions': 0.31; 'this.': 0.32; 'another': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'could': 0.34; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; 'options:': 0.36; 'url:listinfo': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'e.g.': 0.38; 'does': 0.39; 'url:mail': 0.40; 'hope': 0.61; 'first': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'incoming': 0.72; 'day': 0.76; '3.4': 0.84; 'captures': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=S7fhGAksI6l2BT85YefL2mNb+SpHEztSkBYC3XghJzo=; b=B7GwMQPbqmStcaiX9rfRTbFMjrGEzjALJxhsobTNfyn4kNvM0bMDEyCb5NVmSBbCa/ iKDivBql0cVZlD6EDB4jpfmgJ22V/tt+7feDm7AQNiqLjRhIJULj6Kv+xt3LM9AMcErh tIa69xRjG1b1jqeEKQU3U8k+AuDTpZssnKjCazXw+W3C+Tc/5ywW+9wXhKGhv/365JVh 0B+wiGdysqeX/omtAXWvlH34r1utMqD3cWeZtLi7CAUI1plbC1opXJ8JLo94PHLqv0ha 4RFbEXI3HwlumxyNUzqyPUJcrf0MJftY2sDGoVoC2++1587oO41CvCAw8PcWlF8BFAiV ksjA==
MIME-Version 1.0
X-Received by 10.152.115.131 with SMTP id jo3mr10616084lab.20.1414580970926; Wed, 29 Oct 2014 04:09:30 -0700 (PDT)
In-Reply-To <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com>
References <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com>
Date Wed, 29 Oct 2014 12:09:30 +0100
Subject Re: Python Style Question
From Rafael Romero Carmona <rafaelromcar@gmail.com>
To Anton <anton.schattenfeld@gmail.com>
Content-Type text/plain; charset=UTF-8
X-Mailman-Approved-At Wed, 29 Oct 2014 12:43:21 +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 <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.15295.1414583003.18130.python-list@python.org> (permalink)
Lines 109
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414583003 news.xs4all.nl 2950 [2001:888:2000:d::a6]:50814
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79908

Show key headers only | View raw


Hi, first in Python 2.7.6 and Python 3.4.0 list haven't got any add
function but they have append.

I think you could do better with something like

==========
import json
l = [1, -1, 0, '1', '-1', '0', json.dumps(-1), json.dumps(1),
json.dumps(0), 'x', 'sqjklsqjk__', (1, 2)]

values = []

for c in l:
    try:
        c_int = int(c)
    except ValueError:
        pass
    except TypeError:
        pass
    else:
        values.append(c_int)
        continue
print(values)
==========

The code has been tested in Python 2.7.6 and 3.4 and returns [1, -1,
0, 1, -1, 0, -1, 1, 0]

You don't need to do two try because you can process both exceptions
in the same way. You don't really need to do json.loads because if you
have a json string which is an integer, you could do that directly
with int(c) which can take a string and transform in an integer.

With ValueError it captures the exception when it tries to transform
the characters' strings and with TypeError it captures the exception
when it tries to work with the tuples.

Have a good day and hope it works for you!

2014-10-29 11:42 GMT+01:00 Anton <anton.schattenfeld@gmail.com>:
> 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.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

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