Common ways to write configuration files in Python

Love to learn 2021-01-20 19:47:55
common ways write configuration files


file

Hello everyone , Let's talk today Python Configuration file in How to write ?.

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.1
user = root
password = 123456
port = 3306
database = 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 pymysql
import pymysql
from configparser import ConfigParser
cfg = 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 : file

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 = 3306
database = "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 ……).

file

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 .

author :100gle source :Python The Chinese community

[ Take it with you !Python 3.9 Official Chinese documents , Time limited collection !] (http://dwz.date/dE6v)

[ Time limit ! Quick collar !14 Zhang HD Python Quick reference table , It is necessary to improve efficiency !] (http://dwz.date/dE6w)

[GitHub Star sign 3W+,80 individual Python Case study , Take you easy to play Python Study !] (http://dwz.date/dE64)

版权声明
本文为[Love to learn]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210120180147540C.html

  1. A series of problems and solutions in Java calling Python
  2. python自动化爬取淘宝商品数据导入execl表格
  3. Using Python to automatically punch in the pin / enterprise wechat
  4. 【分享】python+requests接口测试基础
  5. Python automatically crawls Taobao product data and imports it into excel table
  6. C++/Python描述 628. 三个数的最大乘积
  7. Python的容器有哪些?分别有什么作用?
  8. python+requests接口测试基础
  9. 20 行代码:Serverless 架构下用 Python 轻松搞定图像分类和预测
  10. python+requests接口测试基础
  11. [share] Python + requests interface test foundation
  12. C + + / Python description 628. Maximum product of three numbers
  13. What are Python containers and what are their functions?
  14. Testing foundation of Python + requests interface
  15. 20 lines of code: easy to do image classification and prediction with Python under serverless architecture
  16. Python爬取优质高清壁纸网站:彼岸
  17. Testing foundation of Python + requests interface
  18. 【人生苦短,我学 Python】基础篇——列表(Day8)
  19. Python crawls high quality HD Wallpaper website: the other side
  20. Python图像增强与特效-利用百度AI进行黑白图像上色
  21. 【七天搞定Python】day01.Python环境配置、pip、IDE、注释、变量,数据类型、标识符/关键字、输出、输入
  22. Life is short, I learn Python
  23. Python image enhancement and special effects - using Baidu AI to color black and white images
  24. Python environment configuration, Pip, IDE, comment, variable, data type, identifier / keyword, output, input
  25. 为什么说Python是最伟大的语言?看图就知道了 - 知乎
  26. Why is Python the greatest language? Just look at the picture. - Zhihu
  27. 通过创建视频游戏来学习 Python
  28. Learn Python by creating video games
  29. Python3版本下创建计算给定日期范围内工作日方法
  30. Creating a method to calculate working days within a given date range in Python 3
  31. 图解爬虫,用几个最简单的例子带你入门Python爬虫
  32. Graphical crawler, with a few of the simplest examples to take you to the introduction of Python crawler
  33. python+requests基础知识
  34. Basic knowledge of Python + requests
  35. python自定义windowsr日志支持文件分割
  36. python+requests基础知识
  37. Python custom Windowsr log supports file segmentation
  38. Basic knowledge of Python + requests
  39. 高级测试 | Python笔试题
  40. 火了!开源的 Python 抢票神器,过年回家就看这一波了!
  41. Python 爬虫进阶 - 前后端分离有什么了不起,过程超详细!
  42. 【python】使用pip提示ModuleNotFoundError
  43. 【python】虚拟环境搭建
  44. Advanced test | Python written test questions
  45. Fire! Open source Python ticket grabbing artifact, come home to see this wave of New Year!
  46. Python crawler advanced - before and after the end of the separation of what great, super detailed process!
  47. [Python] prompt modulenotfounderror with PIP
  48. Building a virtual environment
  49. Serverless 架构下用 Python 轻松搞定图像分类和预测
  50. Easy image classification and prediction with Python under serverless architecture
  51. python协程爬取某网站的老赖数据
  52. Python coroutine crawls Laolai data of a website
  53. 使用Python分析姿态估计数据集COCO的教程
  54. Using Python to analyze the data set coco of attitude estimation
  55. win环境 python3 flask 上手整理 环境搭建(一)
  56. Getting started with win environment python3 flash
  57. Python实现一个论文下载器,赶紧收藏
  58. win环境 python3 flask 上手整理 快速上手-基础操作(二)
  59. Python 中常见的配置文件写法
  60. Python to achieve a paper Downloader, quickly collect