Python Config Parser (Duplicate Key Support)

后端 未结 2 1433
栀梦
栀梦 2020-12-12 06:13

So I recently started writing a config parser for a Python project I\'m working on. I initially avoided configparser and configobj, because I wanted to support a config file

2条回答
  •  遥遥无期
    2020-12-12 07:15

    Well I would certainly try to leverage what is in the standard library if I could.

    The signature for the config parser classes look like this:

    class ConfigParser.SafeConfigParser([defaults[, dict_type[, allow_no_value]]])

    Notice the dict_type argument. When provided, this will be used to construct the dictionary objects for the list of sections, for the options within a section, and for the default values. It defaults to collections.OrderedDict. Perhaps you could pass something in there to get your desired multiple-key behavior, and then reap all the advantages of ConfigParser. You might have to write your own class to do this, or you could possibly find one written for you on PyPi or in the ActiveState recipes. Try looking for a bag or multiset class.

    I'd either go that route or just suck it up and make a list:

    foo = value1, value2, value3
    

提交回复
热议问题