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
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
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
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
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
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
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
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
[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
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)
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
'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
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 :
>
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
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
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
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
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
31 matches
Mail list logo