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


Groups > comp.lang.python > #79998

Re: Python Style Question

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: Python Style Question
Date 2014-10-31 04:55 +0000
Organization A noiseless patient Spider
Message-ID <m2v4o2$mle$1@dont-email.me> (permalink)
References <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com> <54521c8f$0$12982$c3e8da3$5496439d@news.astraweb.com> <mailman.15318.1414671698.18130.python-list@python.org> <5452c02a$0$12994$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


On Fri, 31 Oct 2014 09:48:10 +1100, Steven D'Aprano wrote:

> MRAB wrote:

>> How about:
>> 
>> int(str(obj).strip('"'))
> 
> Absolutely not.
> 
> obj = '""""""""""""""1\n\n\n\n'  # not valid JSON load_int(obj)
> => raises ValueError int(str(obj).strip('"'))
> => wrongly returns 1

How about

#!/usr/bin/python

import re, json

l = [1, -1, 0, '+2', '2', '-2', '0', '"+3"', '"3"', '"-3"', '"0"', 
     json.dumps(-4), json.dumps(4), json.dumps(0), 
     'x', 'sqjklsqjk__', (5, 6), 
     7.7, -7.7, '8.8', '+8.8', '-8.8', '"9.9"', '"+9.9"', '"-9.9"']

patt1 = re.compile(r'^([-+]?\d+)$')
patt2 = re.compile(r'^"([-+]?\d+)"$')

def getTheInt(x):

    if isinstance(x,int):
        return x

    if isinstance(x,str):
        tmp = patt1.match(x)

        if tmp:
            return int(tmp.group(1))

        tmp = patt2.match(x)

        if tmp:
            return int(tmp.group(1))

    return None

a = []

for n in l:
    a.append(getTheInt(n))

print a

# end of code

prints:

[1, -1, 0, 2, 2, -2, 0, 3, 3, -3, 0, -4, 4, 0, None, None, None, None, 
None, None, None, None, None, None, None]

I know re matching the strings may be overkill, but it may be the best 
way of checking that the string contains the expected character format to 
convert to an int.

-- 
Denis McMahon, denismfmcmahon@gmail.com

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