Config files with different types

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Fri Jul 3 00:39:46 EDT 2009


In message <mailman.2506.1246557392.8015.python-list at python.org>, Zach 
Hobesh wrote:

> I want to be able to look at the value and determine what type it
> SHOULD be.  Right now, configs['overwrite'] = 'true' (a string) when
> it might be more useful as a boolean.

Typically the type should be what you expect, not what is given. For 
example, it doesn't make sense for your configs["overwrite"] setting to be 
"red", does it?

A reasonable solution might be to have a table of expected types for each 
config item keyword, e.g.

    configs_types = \
        {
           ...
           "overwrite" : lambda x : x[0] not in set("nNfF"),
           ...
        }

Then you can just do something like

    configs[keyword] = configs_types[keyword](configs[keyword])

with appropriate trapping of ValueError exceptions.




More information about the Python-list mailing list