Teach you how to write a configuration script engine in Python (benefits)

Copyright statement: originality is not easy, this article prohibits plagiarism, reprinting must attach a link, infringement must be investigated!

1. Write configuration information

Configuration information initialization
Define configuration engine class and initialization method, which has two properties, configuration instance object and configuration file path:

import configparser

class ConfigEngine(object):

    def __init__(self, config_path):
        self.cf = configparser.ConfigParser()
        self.config_path = config_path

Write the configuration information into the configuration file. This method has three formal parameters, category (configuration information category), name (configuration field name), value (configuration field value):

def write_to_config(self, category, name, value):
    self.cf.add_section(category)
    self.cf.set(category, name, value)
    self.cf.write(open(self.config_path, "w+"))

For example, now we want to write the configuration information of the mailbox into the configuration file config.ini. Taking Ali mailbox as an example, we can call it like this:

if __name__ == '__main__':
  config_path = 'config.ini'
  config_engine = ConfigEngine(config_path)
  config_engine.write_to_config('EMAIL', 'host', 'smtp.qiye.aliyun.com')
  config_engine.write_to_config('EMAIL', 'port ', '25')
  ………………

View configuration file information:
insert image description here

2. Read configuration information

Read the configuration information in the configuration file config.ini. This method has two parameters, category (configuration information category), name (configuration field name):

def get_value(self, category, name):
    try:
        self.cf.read(self.config_path, encoding='gbk')
        return self.cf.get(category, name)
    except KeyError:
        print('读取失败!')
        return 'error'

The calling method is similar to the above, just pass in the corresponding parameters

3. Modify configuration information

Modify the configuration information in the configuration file config.ini. The parameters of this method are the same as those of the configuration information writing method. There is no add_section() method, and a read() method is added:

def modify_config(self, category, name, value):
    self.cf.read(self.config_path)
    self.cf.set(category, name, value)
    self.cf.write(open(self.config_path, "w+"))

The calling method is the same as above, just pass in the corresponding parameters

Fourth, the configuration engine summary

This article describes how to use the library configparser to write a configuration script engine, which can

separate important configuration data, improve programming efficiency and reduce later maintenance . When configuration information changes, such as a customer's mailbox information changes, the configuration data When it is relatively small, we can manually open the configuration file to change, but when the amount of data is very large, we may need to call the script to change the configuration information In fact, this configuration script engine is a layer of encapsulation, we only need to write the

least code , import This configuration engine package can realize the required function by calling the corresponding method and passing in the parameters. It is simple and convenient. It can be used as a dependency package and used in conjunction with the mailbox script engine.

5. Author Info

Author: Xiaohong's fishing routine, Goal: Make programming more interesting!

Focus on algorithms, reptiles, websites, game development, data analysis, natural language processing, AI, etc., looking forward to your attention, let us grow and code together!

Copyright Note: This article prohibits plagiarism and reprinting, and infringement must be investigated!

Guess you like

Origin blog.csdn.net/qq_44000141/article/details/130854168