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