srtring to int question
Darrell Gallion
darrell at dorb.com
Sun Aug 13 11:51:03 EDT 2000
"Bernhard Herzog" wrote:
> Using assert for this is a bad idea. Assert statements will be omitted
> if the code is optimized (Python's -O option). They're meant to check
> internal program state and invariants, not to validate user input.
>
Your right.
import re, sys
globalNames={'hex':__builtins__.hex, 'int':__builtins__.int,
'raw_input':__builtins__.raw_input}
def convert(s):
try:
exec(s,globalNames, globalNames)
return globalNames.get("x",None)
except:
import traceback
traceback.print_exc ()
def convert1(s):
if re.match("(\s*[-+*\\=]?\s*\d+\.?\d*\s*)+",s):
try:
return eval(s,globalNames, globalNames)
except:
import traceback
traceback.print_exc ()
> The regex used has a bug, btw. It doesn't allow signs, e.g. "-1" will
> not be matched.
>
Not a bug, since Julian dates can't be negative. These instant code examples
are nice distractions from the hard problems I supposed to be working on :)
--Darrell
More information about the Python-list
mailing list