程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The common writing methods of Python configuration files are combed and summarized

編輯:Python

Catalog

Preface

Why write configuration files

ini

json

toml

yaml

ending

Preface

The complexity of the configuration file types listed in this article increases from top to bottom :ini < json ≈ toml < yaml, There are advantages and disadvantages between them , You can choose according to your actual needs and team cooperation requirements .

Why write configuration files

In the development process , We often use some fixed parameters or constants . For these more fixed and commonly used parts , It is often written to a fixed file , Avoid repetition in different module code to keep the core code clean .

This fixed file can be written directly into a .py file , for example settings.py or config.py, The advantage of this is that it can directly pass through under the same project import To import parts of it ; But if we need to be in other non Python When sharing configuration files on the platform of , Write it as a single .py It's not a good choice .

In this case, we should choose the general configuration file type to store these fixed parts . At present, the commonly used and popular configuration file format types are mainly ini、json、toml、yaml、xml etc. , These types of configuration files can be parsed through standard library or third-party library .

ini

ini namely Initialize Initialization means , Early on Windows The storage format of the configuration file on the .ini The writing of the document is easy to understand , It's often simpler , Usually by the section (Section)、 key (key) And the value (value) form , It's like this :

[localdb]host = 127.0.0.1user = rootpassword = 123456port = 3306database = mysql

Python Built in configparser Standard library , We can directly use it on ini File parsing . If we save the above content in a name called db.ini In the file of , And then use read() Method to parse and read , Finally through items() Method to get all the key value pairs under the specified node .

>>> from configparser import ConfigParser>>> cfg = ConfigParser()>>> cfg.read("/Users/Bobot/db.ini")['/Users/Bobot/db.ini']>>> cfg.items("localdb")[('host', '127.0.0.1'), ('user', 'root'), ('password', '123456'), ('port', '3306'), ('database', 'mysql')]

It should be noted that ,configparser By default, the value is rendered as a string , So that's why we're in db.ini There are no quotation marks in the document, but the reason why the literal quantity is written directly on it .

After getting the key value pair , I actually converted it into a dictionary , And then through unpacking the way to wear reference , Keep the code simple :

#!pip install pymysqlimport pymysqlfrom configparser import ConfigParsercfg = ConfigParser()cfg.read("/Users/Bobot/db.ini")db_cfg = dict(cfg.items("localdb"))con = pymysql.connect(**db_cfg)json

json Format can be said to be a common form of documents , It is also a popular data exchange format on the Internet . besides ,json Sometimes it's a configuration file .

such as npm(JavaScript Package management tools are similar to Python Of pip)、 And Microsoft's widely used VSCode Editor , All use json Write configuration parameters .

and configparser equally ,Python Also built in json Standard library , Can pass load() and loads() Method to import files and strings json Content .

{ "localdb":{ "host": "127.0.0.1", "user": "root", "password": "123456", "port": 3306, "database": "mysql" }}

We save the above as db.json After reading and parsing ,json Library to read json The file is relatively simple and easy , And it's easy to parse into Python The dictionary object of .

>>> import json>>> from pprint import pprint>>>>>> with open('/Users/Bobot/db.json') as j:... cfg = json.load(j)['localdb']...>>> pprint(cfg){'database': 'mysql','host': '127.0.0.1','password': '123456','port': 3306,'user': 'root'}

Use json The disadvantage of file configuration is that syntax standards are strictly limited , One of the criticisms is that you can't write notes in it , Unless you take json Other supersets of type as alternatives (VSCode You can write notes in json The parameter configuration file is one of the alternatives ); At the same time, there is the problem of too deep nesting , It's easy to make mistakes , It should not be used to write too long or complex parameter configuration information .

toml

toml Format ( or tml Format ) yes Github cofounder Tom Preston-Werner A configuration file format is proposed . According to Wikipedia ,toml It was first put forward in 2013 year 7 month , Seven years ago ; It's also in some ways related to what we're going to talk about later yaml The document is somewhat similar to , But if you know yaml There are dozens of pages in the specification of ( No mistake. , It's really dozens of pages ……) When , Maybe you really don't want to write such a complicated configuration file ,toml The format is a good choice .

toml The format is roughly as follows :

You can see it here toml It's kind of similar to what I said before ini file . But it's better than ini Expanded more content .

In the sample image, we can see , In addition to the basic string , For example, time stamp 、 Boolean value 、 Arrays and so on are further supported , And the style and Python The original way of writing is very similar .

Of course, there won't be too much introduction here toml Some specifications of the format , Someone has translated the official specification document , Interested friends can check directly .

It's a good fit Python The configuration file type has been created by developers 「 wheel 」, Currently in Github On Stars The largest number is uiri/toml Version of , But the version only passed v0.5 edition toml standard , But it is very simple in use , We can go through pip Command to install

pip install toml

The parsing of this library is very simple , It's a little bit like json The analytic usage of the library , That is, through load() or loads() To parse ; In the same way, the same is true of conversion and export .

For example, we now write the following into config.toml in :

[mysql]host = "127.0.0.1"user = "root"port = 3306database = "test" [mysql.parameters] pool_size = 5 charset = "utf8" [mysql.fields] pandas_cols = [ "id", "name", "age", "date"]

And then we can go through toml In the library load() Method to read :

>>> import toml>>> import os>>> from pprint import pprint>>> cfg = toml.load(os.path.expanduser("~/Desktop/config.toml"))>>> pprint(cfg){'mysql': {'database': 'test', 'fields': {'pandas_cols': ['id', 'name', 'age', 'date']}, 'host': '127.0.0.1', 'parameters': {'charset': 'utf8', 'pool_size': 5}, 'port': 3306, 'user': 'root'}}

You can see toml Files are indirectly converted to dictionary types , Of course, this is also json How to write the version ( Replace single quotation marks with double quotation marks ), It is convenient for us to call or pass parameters later .

yaml

yaml Format ( or yml Format ) It is a popular configuration file at present , It was as early as 2001 By a person named Clark Evans The people who put forward ; At the same time, it is also a widely used configuration file type , Typical is Docker In the container docker-compose.yml The configuration file , If you use Docker It's no stranger to those who deploy .

yaml The design of the document is from Python、XML And other places to get inspiration , So you can see the shadow of these parts clearly when you use it .

In the last section toml I mentioned in the content that ,yaml The content of the specification can be said to be lengthy and complex , A good 80 There are so many pages ( Fight the strong , Terror is like this ……).

yaml Standard number of pages :

So interested friends can learn the relevant usage by themselves .

YAML Officials have already provided the corresponding Python Library to support , namely PyYAML; Of course, we also need to install it in advance :

pip install pyyaml

Same as json Kuhe toml Like the library , adopt load() Method to load .

It should be noted that , Use load() There are some security risks in this method , From Cisco Talos In this report, we can see that , If unknown or untrusted yaml file , Then there may be the risk of being attacked and network security risks , Because it can directly call the corresponding Python Function to execute the command needed by the attacker , For example, in yaml Write this paragraph in the file :

# Use Linux and macOS Don't try it easily !!python/object/apply:os.system ["rm -rf /"]

So it's better to use safe_load() Instead of load() Method .

This sum Python Built in string Standard library Template Class substitute() Template method has the same security risks , So use safe_substitute() It's the same thing to replace .

For example, we will now write some of the previous configuration information to config.yaml In file :

mysql: host: "127.0.0.1" port: 3306 user: "root" password: "123456" database: "test" parameter: pool_size: 5 charset: "utf8" fields: pandas_cols: - id - name - age - date

And then we passed safe_load() Method :

>>> import os>>> from pprint import pprint>>>>>> with open(os.path.expanduser("~/config.yaml"), "r") as config:... cfg = yaml.safe_load(config)...>>> pprint(cfg){'mysql': {'database': 'test', 'fields': {'pandas_cols': ['id', 'name', 'age', 'date']}, 'host': '127.0.0.1', 'parameter': {'charset': 'utf8', 'pool_size': 5}, 'password': '123456', 'port': 3306, 'user': 'root'}}

You can see the final result and the preceding toml The analysis results of the library are basically consistent .

ending

This article lists some mainstream and common configuration file types and their Python The reading method of , Some readers may find that there is no xml Format type content . about xml The configuration file may be associated with Java Friends who deal with language will meet more , but xml The readability of the document is really daunting ; Yes xml Friends who don't know the file can use Chrome The browser randomly enters a website and presses F12 After entering the developer, check out the dense html The element is .xml The epitome of .

In addition to these mainstream profile types , Like some .cfg、.properties Can be used as a configuration file , Even as mentioned at the beginning , You use one alone .py File to write all kinds of configuration information as a configuration file to import is no problem , It's just that there may be barriers to cross language sharing . Therefore, this article only introduces , Interested friends can learn more about .

The complexity of the configuration file types listed in this article increases from top to bottom :ini < json ≈ toml < yaml, There are advantages and disadvantages between them , You can choose according to your actual needs and team cooperation requirements .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved