new tasks

This commit is contained in:
Ingy döt Net 2013-04-09 00:46:50 -07:00
parent 2a4d27cea0
commit 80737d5a6a
1194 changed files with 15353 additions and 1 deletions

View file

@ -0,0 +1,3 @@
s = '123'
if s.isdigit():
# numeric

View file

@ -0,0 +1,28 @@
def is_numeric(lit):
'Return value of numeric literal string or ValueError exception'
# Handle '0'
if lit == '0': return 0
# Hex/Binary
litneg = lit[1:] if lit[0] == '-' else lit
if litneg[0] == '0':
if litneg[1] in 'xX':
return int(lit,16)
elif litneg[1] in 'bB':
return int(lit,2)
else:
try:
return int(lit,8)
except ValueError:
pass
# Int/Float/Complex
try:
return int(lit)
except ValueError:
pass
try:
return float(lit)
except ValueError:
pass
return complex(lit)

View file

@ -0,0 +1,16 @@
>>> for s in ['0', '00', '123', '-123.', '-123e-4', '0123', '0x1a1', '-123+4.5j', '0b0101', '0.123', '-0xabc', '-0b101']:
print "%14s -> %-14s %s" % ('"'+s+'"', is_numeric(s), type(is_numeric(s)))
"0" -> 0 <type 'int'>
"00" -> 0 <type 'int'>
"123" -> 123 <type 'int'>
"-123." -> -123.0 <type 'float'>
"-123e-4" -> -0.0123 <type 'float'>
"0123" -> 83 <type 'int'>
"0x1a1" -> 417 <type 'int'>
"-123+4.5j" -> (-123+4.5j) <type 'complex'>
"0b0101" -> 5 <type 'int'>
"0.123" -> 0.123 <type 'float'>
"-0xabc" -> -2748 <type 'int'>
"-0b101" -> -5 <type 'int'>
>>>

View file

@ -0,0 +1,5 @@
s = '123'
try:
i = float(s)
except ValueError, TypeError:
print 'not numeric'