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:\\SDS2_7.0\\macro\\macrolib' not in sys.path:

    sys.path.append('C:\\SDS2_7.0\\macro\\macrolib')

   

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:

        del test_convertType

        del convertType

 

"""

>>> 1 <type 'int'>

15.7376 <type 'float'>

Python <type 'str'>

0.25 <type 'float'>

153.06640625 <type 'float'>

>>>"""