Automagical Configuration Data Conversion in Python

The trusty old “INI” file is a handy way to organize configuration data as key:value pairs, and Python provides built-in support for creating, reading and modifying these types of files. The downside is that the data is all in ASCII form. In order to get the data from the file into your application it must be converted to the appropriate type. Here is the official Python documentation for the included ConfigParser.

Below is a simple function that will attempt to convert data captured in string form to a valid Python data type. It can handle integers, floating point, tuples, lists and dictionaries. Actual strings, and binary values (actual integers, etc.) are passed through as-is. It should be safe to put it into a datastream and not worry that something will get mangled or lost. If the function is unable to determine what the input is, it will simply pass it along without change.

def AutoConvert(input):
    """ Attempts to identify and convert any type of input to a standard
        Python type.

        Useful for dealing with values stored in a configuration data file
        (i.e. an 'ini' file) where the values read from the KV pairs are
        always in string format.

        Returns the converted value (or just the input value, if no conversion
        took place) along with the data type as a 2-tuple. Always returns
        valid data, even if it's just a copy of the original input. Never
        returns None.

        TODO: Add ability to specify desired type as an input parameter so
              routine can be directed to do hex to int conversions, for
              example, or handle values in particular units.

        April 2009 - JMH
    if type(input) == str:
        if input.isalpha():
            # convert T and F characters to 1 and 0
            if len(input) == 1 and input.upper() == 'T':
                ret_val = 1
            elif len(input) == 1 and input.upper() == 'F':
                ret_val = 0
                if input.upper() == "TRUE":
                    ret_val = 1
                elif input.upper() == "FALSE":
                    ret_val = 0
                    ret_val = input
        elif input.isdigit():
            # integer in string form, convert it
            ret_val = int(input)
        elif input.isalnum():
            # mixed character string, just pass it on
            ret_val = input
            # see if string is a float value, or something else
                ret_val = float(input)
                # no, see if it's a tuple, list or dictionary
                    ret_val = eval(input)
                    ret_val = input # don't know what it is
        # we'll assume that input is a valid type not captured in a string
        ret_val = input

    ret_type = type(ret_val)

    return ret_val, ret_type

Please note that the function returns a 2-tuple that includes both the value (converted or not, as the case may be) and data type according to Python. If a conversion fails this can be checked to determine if further processing is necessary.


1 Response to “Automagical Configuration Data Conversion in Python”

  1. 1 INI Files « Crankycode Trackback on July 7, 2009 at 11:47 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Follow Crankycode on

Little Buddy

An awesome little friend

Jordi the Sheltie passed away in 2008 at the ripe old age of 14. He was the most awesome dog I've ever known.


%d bloggers like this: