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


Groups > comp.lang.python > #79981

Re: Python Style Question

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Python Style Question
Date 2014-10-30 20:34 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-7698C6.20335730102014@news.panix.com> (permalink)
References <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com> <54521c8f$0$12982$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


In article <54521c8f$0$12982$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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__)

Depending on how strictly you're trying to do input validation, the 
int(json.loads(obj)) may not be what you want.  It allows well-formed 
json encoding floats, for example.

And, of course, since

>>> isinstance(True, int)
True

this code accepts booleans.  Oh, but wait, that's by design :-)

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