Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Steven D'Aprano
Roy Smith wrote: Do you really want to except SystemExit, KeyboardInterrupt, MemoryError and SyntaxError? Absolutely. Let's take my example -- you're writing software for a Mars Rover. I have no idea how you might get a MemoryError, but let's say you do. Which would you rather do,

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Roy Smith
In article 01aaa1da$0$20629$c3e8...@news.astraweb.com, Steven D'Aprano st...@pearwood.info wrote: Okay, but that surely falls under chapter 18 of the Advanced Python Programming for the Mars Rover book rather than chapter 5 of Newbies Guide to Python. Well, sure, but this thread started out

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Floris Bruynooghe
On Feb 16, 12:05 am, Mel mwil...@the-wire.com wrote: Christian Heimes wrote: Roy Smith wrote: They make sense when you need to recover from any error that may occur, possibly as the last resort after catching and dealing with more specific exceptions. In an unattended embedded system

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Floris Bruynooghe
On Feb 16, 7:09 am, Python Nutter pythonnut...@gmail.com wrote: silly me, forgot to mention build a set from digits + '.' and use that for testing. `.' is locale dependent. Some locales might use `,' instead and maybe there's even more out there that I don't know of. So developing this

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Paddy O'Loughlin
2009/2/16 Python Nutter pythonnut...@gmail.com: silly me, forgot to mention build a set from digits + '.' and use that for testing. Cheers, PN 2009/2/16 Python Nutter pythonnut...@gmail.com: Type casting seems to be the wrong way to go about this. teststring = '15719'

Re: Pythonic way to determine if a string is a number

2009-02-17 Thread python
Original poster here: Just for the record, my *original* post did include an explicit trapping of the ValueError exception. :) My point is that the coaching offered by this forum does not always fall on deaf ears. Thanks for everyone's help on this and all the other posts in this forum.

[Fwd: Re: Pythonic way to determine if a string is a number]

2009-02-16 Thread Tim Golden
[Resending after a bounce from mailing list] pyt...@bdurham.com wrote: try: float (input) except ValueError: return False else: return True I follow the semantics, but I don't know why I would prefer the try/else technique over the simpler: try: float( input ) return True

Re: Pythonic way to determine if a string is a number

2009-02-16 Thread Nick Craig-Wood
pyt...@bdurham.com pyt...@bdurham.com wrote: Thanks for everyone's feedback. I believe my original post's code (updated following my signature) was in line with this list's feedback. Christian: Thanks for reminding me about exponential formats. My updated code accounts for these type of

Re: Pythonic way to determine if a string is a number

2009-02-16 Thread Paddy
On Feb 15, 5:46 pm, pyt...@bdurham.com wrote: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. I searched the string and cMath libraries for a similar function without success. I can think of at least 3 or 4 ways to build my own

Pythonic way to determine if a string is a number

2009-02-15 Thread python
What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. I searched the string and cMath libraries for a similar function without success. I can think of at least 3 or 4 ways to build my own function. Here's what I came up with as a

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Christian Heimes
pyt...@bdurham.com schrieb: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. I searched the string and cMath libraries for a similar function without success. I can think of at least 3 or 4 ways to build my own function. Here's what

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Philip Semanchuk
On Feb 15, 2009, at 12:46 PM, pyt...@bdurham.com wrote: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. try: int(number) is_an_int = True except: is_an_int = False try: float(number) is_a_float = True except:

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Roy Smith
In article mailman.9571.1234720024.3487.python-l...@python.org, pyt...@bdurham.com wrote: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. try: int(myString) except ValueError: print That's bogus, man --

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Christian Heimes
Philip Semanchuk schrieb: On Feb 15, 2009, at 12:46 PM, pyt...@bdurham.com wrote: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. try: int(number) is_an_int = True except: is_an_int = False Please don't teach new

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread MRAB
Roy Smith wrote: In article mailman.9571.1234720024.3487.python-l...@python.org, pyt...@bdurham.com wrote: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. try: int(myString) It could be a float, so: float(myString) This will

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Roy Smith
In article mailman.9577.1234722607.3487.python-l...@python.org, Christian Heimes li...@cheimes.de wrote: Philip Semanchuk schrieb: On Feb 15, 2009, at 12:46 PM, pyt...@bdurham.com wrote: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread python
Thanks for everyone's feedback. I believe my original post's code (updated following my signature) was in line with this list's feedback. Christian: Thanks for reminding me about exponential formats. My updated code accounts for these type of numbers. I don't need to handle inf or nan values. My

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Christian Heimes
Roy Smith wrote: I agree that the bare except is incorrect in this situation, but I don't agree that you should *never* use them. A bare except should be used when followed by a raise try: func() except: log_error() raise They make sense when you need to recover from any error that

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Tim Golden
pyt...@bdurham.com wrote: code # str_to_num.py def isnumber( input ): try: num = float( input ) return True except ValueError: return False Just for the info, Malcolm, you don't actually need to assign the result of float (input) to anything if you don't need

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread python
Tim, Just for the info, Malcolm, you don't actually need to assign the result of float (input) to anything if you don't need to use it. All you're looking for is the exception. Let the intepreter convert it and then throw it away. Yes! Also, as an alternative style which can be more

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Mel
Christian Heimes wrote: Roy Smith wrote: They make sense when you need to recover from any error that may occur, possibly as the last resort after catching and dealing with more specific exceptions. In an unattended embedded system (think Mars Rover), the top-level code might well be:

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Scott David Daniels
pyt...@bdurham.com wrote: Thanks for everyone's feedback def isnumber( input ): try: num = float( input ) return True except ValueError: return False Pretty good, but what about 0x23? def isnumber( input ): try: num = float(input)

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Philip Semanchuk
On Feb 15, 2009, at 1:27 PM, Christian Heimes wrote: Philip Semanchuk schrieb: On Feb 15, 2009, at 12:46 PM, pyt...@bdurham.com wrote: What's the Pythonic way to determine if a string is a number? By number I mean a valid integer or float. try: int(number) is_an_int = True except:

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread John Machin
On Feb 16, 7:05 am, pyt...@bdurham.com wrote: Thanks for everyone's feedback. I believe my original post's code (updated following my signature) was in line with this list's feedback. Christian: Thanks for reminding me about exponential formats. My updated code accounts for these type of

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Roy Smith
In article gnaak5$1ne...@services.telesweet.net, Mel mwil...@the-wire.com wrote: Christian Heimes wrote: Roy Smith wrote: They make sense when you need to recover from any error that may occur, possibly as the last resort after catching and dealing with more specific exceptions. In an

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread python
Scott, Pretty good, but what about 0x23? snipped num = int(input, 0) # Convert to int, base spec in arg Very nice! I wasn't familiar with the use of 0 as a radix value. A quick visit back to the documentation and I'm an enlightened man :) Thanks for your feedback, Malcolm --

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread python
John, Do you care about numbers that are representable as an int, but are treated as inf by float()? For example: | s = '1' * 310 | float(s) | inf | a = int(s) | # OK My code range checks all numbers once they've been parsed, so I don't believe this will be a problem for me.

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Python Nutter
Type casting seems to be the wrong way to go about this. teststring = '15719' teststring.isdigit() returns True That takes care of integers. from string import digits digits '0123456789' now you have all the digits and you can do set testing in your logic to see if the teststring has anything

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Python Nutter
silly me, forgot to mention build a set from digits + '.' and use that for testing. Cheers, PN 2009/2/16 Python Nutter pythonnut...@gmail.com: Type casting seems to be the wrong way to go about this. teststring = '15719' teststring.isdigit() returns True That takes care of integers.

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Tino Wildenhain
Python Nutter wrote: Type casting seems to be the wrong way to go about this. teststring = '15719' teststring.isdigit() returns True Actually its instantiating not type casting and it works by using the type's actual description of the data it accepts. This looks pretty good approach instead

Re: Pythonic way to determine if a string is a number

2009-02-15 Thread Tino Wildenhain
Roy Smith wrote: In article gnaak5$1ne...@services.telesweet.net, Mel mwil...@the-wire.com wrote: Christian Heimes wrote: Roy Smith wrote: They make sense when you need to recover from any error that may occur, possibly as the last resort after catching and dealing with more specific