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. Regards

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

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

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 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 yourself from scratch see

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

2009-02-17 Thread Floris Bruynooghe
On Feb 16, 12:05 am, Mel 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 (think Mars R

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 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 with my tak

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 rathe

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 wa

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

2009-02-16 Thread Nick Craig-Wood
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 numbers. I don

[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 exc

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

2009-02-15 Thread Tino Wildenhain
Roy Smith wrote: In article , Mel 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 (think Mars Rover)

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 o

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

2009-02-15 Thread Python Nutter
'105.22' > teststring2.isalnum() > returns True > > now you can go on from there and test to further to eliminate > 'abcd385laf8' which on alnum() also returns true. > > Have fun, > Cheers, > PN > > > 2009/2/16 : >> What's the

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

2009-02-15 Thread Python Nutter
tstring has anything in digits A dumb way to test is alphanumeric teststring2 = '105.22' teststring2.isalnum() returns True now you can go on from there and test to further to eliminate 'abcd385laf8' which on alnum() also returns true. Have fun, Cheers, PN 2009/2/16 : >

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 p

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

2009-02-15 Thread python
Scott, > Pretty good, but what about 0x23? > > 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 -- http://mail.pytho

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

2009-02-15 Thread Roy Smith
In article , Mel 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 (think Mars Rove

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 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 e

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 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 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 a

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

2009-02-15 Thread Tim Golden
pyt...@bdurham.com wrote: # 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 to u

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 th

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 o

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

2009-02-15 Thread Roy Smith
In article , 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

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

2009-02-15 Thread MRAB
Roy Smith wrote: In article , 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 work even if it's an int. except

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

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

2009-02-15 Thread Roy Smith
In article , 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" -- http://mail.python.org/mailman/listinfo/python-list

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 e

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 bui

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