validation for float input

Hi,

Is a string is valid "float"? Interesting question.

I propose here three functions

def IsFloat2(s):
    try:
        x = float(s)
        return True
    except:
        return False

def IsFloat1(s):
    try:
        return isinstance(float(s), float)
    except:
        return False

def IsFloat(s):
    try:
        x = float(s)
        if 'INF' in str(x):
            return False
        else:
            return True
    except:
         return False

IsFloat2: this is the method proposed by Chris

IsFloat1: same method as IsFloat2 but this one
uses Python 2.3 capability -isinstance-

IsFloat: my favourite, Python 2.3. This is the one
I use in my applications.

Indeed:

With s = '1.0e3333'

IsFloat2(s) returns True
IsFloat1(s) returns True
IsFloat(s) returns False

Note that type(float(s)) is a float:

print type(float(s))

<type 'float'>
but such a value is not very "usefull"

If I need to know if the user has entered a valid integer in a TextCtl,
is use:

def IsInt(s):
    try:
        if float(s) - int(s) != 0:
            return False
        else:
            return isinstance(int(s), int)
    except:
        return False

Any comments about the subject is really wellcome.

Jean-Michel Fauth, Switzerland

Jean-Michel Fauth wrote:

def IsFloat(s):
    try:
        x = float(s)
        if 'INF' in str(x):
            return False
        else:
            return True
    except:
         return False

IsFloat: my favourite, Python 2.3. This is the one
I use in my applications.

Indeed:

With s = '1.0e3333'

IsFloat2(s) returns True
IsFloat1(s) returns True
IsFloat(s) returns False

Well, if you like IEEE 754, "inf" is a valid float! What you want to do with it would be application dependent. Warning, however. The IEEE 754 "special" values (Inf, -Inf, NaN) are not official supported by Python. Instead, your results are dependent on the compiler used to compile Python. For example, on Linux, compiled with gcc, I get:

>>> 1e55555
inf

Note the lower case "inf" rather than the upper case "INF" that Jean-Michel is checking for (I'm guessing Windows). You'd be better off doing somthing like:

PlusInf = 1e1000
MinusInf = -1e1000
NotANumber = PlusInf / MinusInf

print "PlusInf:",PlusInf
print "MinusInf:",MinusInf
print "NotANumber:",NotANumber

#Invalid = [PlusInf, MinusInf, NotANumber]
# NOTE: including NaN does not work, as x == NaN appears to be True for all x !

Invalid = [PlusInf, MinusInf]

def IsFloat(s):
      try:
          x = float(s)
          #print x
          #print Invalid
          if x in Invalid:
              return False
          else:
              return True
      except ValueError:
           return False

for i in ["23.43", "-234e43", "23e43543", "-342-234321", "-34e43212344"]:
     if IsFloat(i):
         print i, "is a Float"
     else:
         print i, "is not a Float"

$ ./junk.py

PlusInf: inf
MinusInf: -inf
NotANumber: nan
23.43 is a Float
-234e43 is a Float
23e43543 is not a Float
-342-234321 is not a Float
-34e43212344 is not a Float

Note that testing for NaN doesn't work, as x == NaN is true for all (or at least many) values of x. I'm not sure is there is a literal that will give you NaN anyway.

If you really want to do this right, or for more detail, read PEP 754:

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hi all (but particualrly Robin),

I've been frequently getting a bunch of warnings when wxInitAllImageHandlers gets called more than once. For the most part I can just remove the extra call, but there are times when this is difficult. If you are writing a module that needs that call, but you don't know how it will be used, you want to put it in your module. However, if other modules do this, you'll get multiple calls. Ideally,

wxInitAllImageHandlers()

could be Replaced by:

wxInitAllImageHandlersIfNeeded()

However, that would have to happen at the wxWindows level, I suppose. An alternative, at the python level, would be to put it into a module:

import wxPyInitAllImageHandlers

which would have a couple lines:

import wx
wx.InitAllImageHandlers()

Then it would only get called on the first import.

Other thoughts?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Robin and I had a discussion about this in the process of migrating the demo
and wx.lib.* libraries to the new namespace a while ago. The official line
is, it is up to the end developer to make that call. It should not be done
at the library level.

Of course, I'd also suggest that the docs for a library reflect that this is
a requirement.

Makes sense, though. For example, to use wx.Bitmap you need to initialize an
image handler of some sort. Same applies to any other library that uses
images at the wx.core level.

···

-----Original Message-----
From: Chris Barker [mailto:Chris.Barker@noaa.gov]
Sent: Thursday, January 08, 2004 17:07
To: wxPython-users@lists.wxwindows.org
Subject: [wxPython-users] wxInitAllImageHandlers called more than once

Hi all (but particualrly Robin),

I've been frequently getting a bunch of warnings when
wxInitAllImageHandlers gets called more than once. For the most part I
can just remove the extra call, but there are times when this is
difficult. If you are writing a module that needs that call, but you
don't know how it will be used, you want to put it in your module.
However, if other modules do this, you'll get multiple calls. Ideally,

wxInitAllImageHandlers()

could be Replaced by:

wxInitAllImageHandlersIfNeeded()

However, that would have to happen at the wxWindows level, I suppose. An
alternative, at the python level, would be to put it into a module:

import wxPyInitAllImageHandlers

which would have a couple lines:

import wx
wx.InitAllImageHandlers()

Then it would only get called on the first import.

Other thoughts?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Chris Barker wrote:

Jean-Michel Fauth wrote:

def IsFloat(s):
    try:
        x = float(s)
        if 'INF' in str(x):
            return False
        else:
            return True
    except:
         return False

How about:
     import math
     def IsFloat(s):
         try:
             x = float(s)
             return x == 0.0 or 0.0 != math.frexp(x)[0]
         except ValueError:
             return False # ValueError either from the float or frexp

-Scott David Daniels
Scott.Daniels@Acm.Org