ConfigParser & converting strings to lists

Bengt Richter bokr at oz.net
Sun Jun 23 02:08:37 EDT 2002


On Sun, 23 Jun 2002 01:53:44 GMT, "Edward K. Ream" <edream at tds.net> wrote:

>Hi all,
>
>I have a question concerning lists in the ConfigParser module.  What I
>would like to do is something like:
>
>config = ConfigParser.ConfigParser()
>config.set("recent files", "recentFiles, files)
>
>where files is a _list_ of file names.  Actually, this works just fine,
Are you sure? I wish you'd just copy and paste from a real interactive session.
It's not really that much more typing ;-)
For 2.2 on windows, I get

 >>> import ConfigParser
 >>> config = ConfigParser.ConfigParser()

Suppose this is your file name list:
 >>> files = ['file1','file2','file3']

You need a section, or you'll get an exception:
 >>> config.add_section('recent files')

Plain files didn't work for me, but the string representation from `files` did:
 >>> config.set("recent files", "recentFiles", `files`)

Which came back as expected:
 >>> config.get("recent files", "recentFiles")
 "['file1', 'file2', 'file3']"

And they try hard to make x == eval(`x`), so to get the actual list, you can:
 >>> eval(config.get("recent files", "recentFiles"))
 ['file1', 'file2', 'file3']
 
Unless the security risk is unacceptable, which it probably is, and you might want
to do something safer. Knowing the repr format for a list of strings, and assumung
the config.set got `alist` as the value, you could, e.g.,
 >>> theFileList = config.get("recent files", "recentFiles")[2:-2].split("', '")
 >>> theFileList
 ['file1', 'file2', 'file3']
But that's pretty brittle, and won't tolerate hand editing very well, so maybe
after checking for '[' to decide it's a list, get the names something like
 >>> [x.strip()[1:-1] for x in theFileList.strip()[1:-1].split(',')]
 ['file1', 'file2', 'file3']


>and the config file contains:
>
>[recent files]
>recentFiles = [file1, file2, ...]
If it really does, it wasn't generated using backquotes like `files`.

>
>as expected.  However,
>
>files = config.get("recent files", "recentFiles")
>
>sets files to the _string_ "[file1, file2, ...]".  I suppose this must
>be how get should work; after all, settings could be arbitrary
>strings...
Again, that wouldn't have come from `files`
>
>Anyway, I wonder whether there is a clever way of converting the string
>representation of a list containing strings into a true list of
>strings.  In any event, wouldn't a getlist convenience method be nice?
>
Ok, here's a go at it (always returns a list):

 >>> class X(ConfigParser.ConfigParser):
 ...     def getlist(self,section,option):
 ...         s = self.get(section, option)
 ...         if '[' in s:
 ...             s = [x.strip()[1:-1] for x in s.strip()[1:-1].split(',')]
 ...         else:
 ...             s = [s.strip()]
 ...         return s
 ...
 >>> configx = X()
 >>> configx.add_section('recent files')
 >>> configx.set("recent files", "recentFiles", `files`)
 >>> configx.get("recent files", "recentFiles")
 "['file1', 'file2', 'file3']"
 >>> configx.getlist("recent files", "recentFiles")
 ['file1', 'file2', 'file3']
 >>> configx.set("recent files", "recentSingleFile", 'a_single_file_name')
 >>> configx.get("recent files", "recentSingleFile")
 'a_single_file_name'
 >>> configx.getlist("recent files", "recentSingleFile")
 ['a_single_file_name']

You could override set so it would automatically do backquotes if the value arg wasn't
already a string, and then call the base class with the resulting string.

And whatever ...

Regards,
Bengt Richter



More information about the Python-list mailing list