[CentralOH] List in config file

William McVey wam at cisco.com
Mon Aug 30 23:36:40 CEST 2010

On Mon, 2010-08-30 at 16:54 -0400, Tony Zhu wrote:
> I need to store some tasks configuration in a config file. 
> But I tried the "ConfigParser" package in python, it does not support
> such lists.
> I thought about xml. Of course it can store such data. But the config
> file is supposed to be edited by hand. xml is not so friendly to a
> notepad. I also checked YAML, plists, json. All of them seem to be too
> complex for my simple requirement.

How are you defining "complex". I would tend to choose YAML for this
kind of config file, and although yaml has support for a lot of
constructs, using the yaml to parse simple (or complex) config files
really isn't complex itself. For instance:

        # Config file demo: foo.yaml
            - tool1
            - tool2
          task2:                     # another way to do it
            tools: [tool2, tool3]

And loading the config:
        >>> import yaml
        >>> f = open("/tmp/foo.yaml")
        >>> doc = yaml.load(f)
        >>> doc['tasks']['task1']
        ['tool1', 'tool2']
        >>> doc['tasks']['task2']['tools']
        ['tool2', 'tool3']
In fact, load() simply builds a structured dictionary that you can
process however you wish:

        >>> doc
        {'tasks': {'task1': ['tool1', 'tool2'], 'task2': {'tools':
        ['tool2', 'tool3']}}}

Hope this helps.

  -- William

More information about the CentralOH mailing list