15

I have something like this in my config file (a config option that contains a list of strings):

[filters]
filtersToCheck = ['foo', '192.168.1.2', 'barbaz']

Is there a more elegant (built-in) way to get a list from filtersToCheck instead of removing the brackets, single-quotes, spaces and then using split() to do that? Maybe a different module?

(Using python3.)

martineau
  • 119,623
  • 25
  • 170
  • 301
tkit
  • 8,082
  • 6
  • 40
  • 71
  • Possible duplicate: http://stackoverflow.com/questions/335695/lists-in-configparser – Mark Hildreth Jul 20 '11 at 08:41
  • 1
    In my opinion, it's not the same.. I know it can be done, but I am looking for a more elegant way to do it.. Besides - that link is around 3 years old, and lot has happened to Python as a language since then. – tkit Jul 20 '11 at 08:47
  • The ConfigParser module hasn't been updated to handle this use case, if that's what you mean. But you can still subclass it to check for `,` in the string and `split` on them if so. – Katriel Jul 20 '11 at 08:52
  • It's exactly the same, solutions that work for the question in the link work also here. Solutions that use json.loads+configParser or ast+configParser are basically one line and work in the general case (e.g. with dictionaries). Unless you find a case for which they fail that is the way to do it. – Vincenzooo Apr 24 '16 at 13:31

2 Answers2

28

You cannot use the python object like a list in the value for the config file. But you can ofcourse have them as comma separated values and once you get it do a split

[filters]
filtersToCheck = foo,192.168.1.2,barbaz

and do

filtersToCheck = value.split(',')

The other approach is ofcourse, subclassing SafeConfigParser class and removing the [ and ] and constructing the list. You termed this as ugly, but this is a viable solution.

The third way is to use Python module as a config file. Projects do this. Just have the filtersToCheck as a variable available from your config.py module and use the list object. That is a clean solution. Some people are concerned about using python file as config file (terming it as security hazard, which is somewhat an unfounded fear), but there also this group who believe that users should edit config files a not python files which serve as config file.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • there are no security concerns in my case. the only problem I see is that I need to make an executable out of my script (using cxfreeze).. I am not sure if a python module (residing in the same folder as the executable) would work in that case? I guess it probably won't since cxfreeze will compile it as well.. – tkit Jul 20 '11 at 08:52
  • does not cxfreeze have any ignore options? – Senthil Kumaran Jul 20 '11 at 09:02
  • didn't think of that! it has "--exclude-modules". I'll try that and report the result. thanks for the idea. – tkit Jul 20 '11 at 09:05
1
ss = """a_string = 'something'
filtersToCheck = ['foo', '192.168.1.2', 'barbaz']
   a_tuple =      (145,'kolo',45)"""

import re
regx = re.compile('^ *([^= ]+) *= *(.+)',re.MULTILINE)


for mat in regx.finditer(ss):
    x = eval(mat.group(2))
    print 'name :',mat.group(1)
    print 'value:',x
    print 'type :',type(x)
    print

result

name : a_string
value: something
type : <type 'str'>

name : filtersToCheck
value: ['foo', '192.168.1.2', 'barbaz']
type : <type 'list'>

name : a_tuple
value: (145, 'kolo', 45)
type : <type 'tuple'>

Then

li = [ (mat.group(1),eval(mat.group(2))) for mat in regx.finditer(ss)]
print li

result

[('a_string', 'something'), ('filtersToCheck', ['foo', '192.168.1.2', 'barbaz']), ('a_tuple', (145, 'kolo', 45))]
eyquem
  • 26,771
  • 7
  • 38
  • 46