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


Groups > comp.lang.python > #79951

Re: Python Style Question

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.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; 'value,': 0.04; 'elif': 0.05; 'subject:Python': 0.06; "'',": 0.07; 'element': 0.07; 'ignored': 0.07; 'json': 0.07; 'subject:Question': 0.07; 'transform': 0.07; 'string': 0.09; '3),': 0.09; 'obj': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'def': 0.12; "%s'": 0.16; '1),': 0.16; 'coerced': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'int):': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'str):': 0.16; 'str,': 0.16; 'wrote:': 0.18; 'version.': 0.19; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'options': 0.25; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'code': 0.31; "d'aprano": 0.31; 'int,': 0.31; 'steven': 0.31; 'this.': 0.32; 'another': 0.32; 'except': 0.35; 'something': 0.35; 'options:': 0.36; 'should': 0.36; 'list': 0.37; 'e.g.': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'incoming': 0.72
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=VomtAoGn c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=aoAcgTy3oA8A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=3EtqsuX_FJ-yCSwxdl8A:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett:2500
Date Thu, 30 Oct 2014 12:21:28 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Python Style Question
References <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com> <54521c8f$0$12982$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <54521c8f$0$12982$c3e8da3$5496439d@news.astraweb.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
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.15318.1414671698.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414671698 news.xs4all.nl 2870 [2001:888:2000:d::a6]:52606
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79951

Show key headers only | View raw


On 2014-10-30 11:10, Steven D'Aprano wrote:
> 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.
>
> I don't particularly like either version. I prefer this:
>
> def load_int(obj):
>      if isinstance(obj, int):
>          # Case 1), an int, e.g. 7
>          return obj
>      elif isinstance(obj, str):
>          # Case 2) and 3), a str or JSON serialised int.
>          # E.g. '7' or '"7"'.
>          try:
>              return int(obj)
>          except ValueError:
>              return int(json.loads(obj))
>      raise TypeError('require int or str, got %s' % type(obj).__name__)
>
[snip]

How about:

int(str(obj).strip('"'))

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