Creating a class with all the elements specified in a file using ConfigParser

后端 未结 3 1426
死守一世寂寞
死守一世寂寞 2020-12-09 07:02

I have created a .ini-like file with all the values which I need later in my program, see below:

[debugging]
checkForAbort = 10
...

[official]
checkForAbort         


        
3条回答
  •  执笔经年
    2020-12-09 07:53

    The Answer by pillmuncher is very helpful and can be easily adapted to use within an existing class. Furthermore, it is also possible to automatically convert the data types using the localconfig module (link). To get these additional functionalities you can use something like:

    from localconfig import LocalConfig
    
    configfile = 'config.ini'
    config = LocalConfig(configfile)
    sections = list(config)
    
    for section in sections:
        items = list(config.items(section))
        CLASS.__dict__.update(items)
    

提交回复
热议问题