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.)
The configparser module has ConfigParser class. It is responsible for parsing a list of configuration files, and managing the parsed database. Return all the configuration section names.
ConfigParser is a Python class which implements a basic configuration language for Python programs. It provides a structure similar to Microsoft Windows INI files. ConfigParser allows to write Python programs which can be customized by end users easily.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With