Back to SDS/2 Parametric Scripts
## typeconv.py Version 1.00 (module 'typeconv')
"""
Convert a string to an integer or float number.
Returns the string if conversion fails
"""
import sys
# append another
path to the system path
if 'C:\\
sys.path.append('C:\\
from macrolib.fifDim
import dim__
def convertType(s):
for func in (int, float, dim__):
try:
n = func(s)
return n
except:
pass
return s
#############################
def test_convertType():
a = convertType("1")
b = convertType("15.7376")
c = convertType("Python")
d = convertType("1/4")
e = convertType("12'-9 17/256")
print a, type(a)
print b, type(b)
print c, type(c)
print d, type(d)
print e, type(e)
#############################
if __name__ == '__main__':
try:
test_convertType()
finally:
"""
>>> 1
<type 'int'>
15.7376 <type
'float'>
Python <type 'str'>
0.25 <type
'float'>
>>>"""