Most Pythonic way to store (small) configuration

marco.nawijn at colosso.nl marco.nawijn at colosso.nl
Tue Aug 4 10:53:21 EDT 2015


On Sunday, August 2, 2015 at 12:14:51 PM UTC+2, Cecil Westerhof wrote:
> There are a lot of ways to store configuration information:
> - conf file
> - xml file
> - database
> - json file
> - and possible a lot of other ways
> 
> I want to write a Python program to display cleaned log files. I do
> not think I need a lot of configuration to be stored:
> - some things relating to the GUI
> - default behaviour
> - default directory
> - log files to display, including some info
>   - At least until where it was displayed
> 
> Because of this I think a human readable file would be best.
> Personally I do not find XML very readable. So a conf or json file
> looks the most promising to me. And I would have a slight preference
> for a json file.
> 
> Any comments, thoughts or tips?
> 
> -- 
> Cecil Westerhof
> Senior Software Engineer
> LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Why not use Python files itself as configuration files?
I have done so several times and I am very pleased with
it. I have tried both INI and JSON, but in the end always
settled on using Python itself. If I have to share the
configuration information with other languages (like C++
or Javascript) I use JSON as the 

For those who are interested, I provide a short summary
below on how I handle configuration files.

The scenario I want to support is that there is a hierarchical
set of configuration files (as typically found in Unix like
systems) that can be stored in a system wide folder (like 
/etc/app), in a user folder (like /home/user/.config/app) and
in a local folder.

Each configuration file has a fixed name, not necessarily ending
in ".py". So for example, the name of the configuration file
is "config.myapp".

Next I start looking for the existence of these files and once
I find them I start merging all the configuration settings into
one Python dictionary. The function that I use can be found below.  
(you need proper import statements, but this is standard Python).

def load_config(config_file, checked=False):
    '''Small utility function to load external configuration files.
    
    '''
    if checked is False:
        if not isfile(config_file) or islink(config_file):
            msg = 'Config file "%s" does not exist.'
            LOGGER.warning(msg)
            return {}

    # Load the module object; we use the imp.load_source function because
    # the module configuration typically has a non-standard file extension
    # (by default the file is called "config.myapp")

    module_name = 'm%s' % str(uuid.uuid1()).replace('-', '')

    # Prevent bytecode generation for configuration files
    sys.dont_write_bytecode = True

    config_module = imp.load_source(module_name, config_file)

    # Continue with bytecode generation for other normal modules
    sys.dont_write_bytecode = False

    if not hasattr(config_module, '__config__'):
        msg = 'Missing "__config__" attribute in configuration file.'
        LOGGER.warning(msg)
        return {}

    else:
        return config_module.__config__




More information about the Python-list mailing list