Efficient configuration storage

Phil Frost indigo at bitglue.com
Wed Sep 22 10:47:41 EDT 2004


One possibility is to use pprint to serialize the dictionary, and then
read it again with eval(). This is more or less human readable, but has
security implications. It could be made somewhat more readable by making
a class who's repr() would return something like "key: <repr(value)>",
and provided a method for evaluating this expression. You might be able
to use the rfc822 module. However, eval() would still be used.

You might be able to use a save flavor of eval, such as presented here:
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286134>.

Otherwise, implementing a class that knows what type a value should be,
then using the right constructor, str(), int(), etc, might be the best
solution.

On Wed, Sep 22, 2004 at 07:32:38AM -0700, sebsauvage wrote:
> Hello.
> 
> In one of my programs ( http://sebsauvage.net/python/webgobbler/ ),
> I have a global dictionnary containing the whole program configuration.
> Sample follows:
> CONFIG = { "network.http.useproxy" : True,
>            "network.http.proxy.address": "proxy.free.fr",
>            "network.http.proxy.port"    : 3128
>            [etc.]
>           }
> 
> I'd like to be able to save/load to/from a file.
> 
> I do not want to use pickle because I want configuration to be human readable.
> 
> ConfigParser could do the trick, but the biggest trouble is that it does
> not retain type (boolean, integer, string...).
> 
> Would I have to store everything as text and cast it everywhere
> it's used (and try/except each cast of course) ?
> 'Looks ugly and inefficient to me.
> 
> Or have a configuration class which knows the type of each parameter
> and casts appropriately from the configuration file ?
> 
> 
> Is there a better way of doing this ?



More information about the Python-list mailing list