


Super complete! Common ways to write configuration files in Python
Apr 13, 2023 am 08:31 AMWhy write configuration files
During the development process, we often use some fixed parameters or constants. For these more fixed and commonly used parts, they are often written into a fixed file to avoid repetition in different module codes and keep the core code clean.
We can write this fixed file directly into a .py file, such as settings.py or config.py. The advantage of this is that we can directly import parts of it through import in the same project; but if we need to When sharing configuration files on other non-Python platforms, writing a single .py is not a good choice.
At this time we should choose a common configuration file type to store these fixed parts. Currently, the commonly used and popular configuration file format types mainly include ini, json, toml, yaml, xml, etc. We can parse these types of configuration files through standard libraries or third-party libraries.
ini
ini means Initialize. In the early days, it was the storage format of configuration files on Windows. The writing method of ini files is easy to understand and is often relatively simple. It usually consists of section, key and value, like the following form:
[localdb] host = 127.0.0.1 user = root password = 123456 port = 3306 database = mysql
Python's own built-in configparser standard library, We can directly use it to parse the ini file. For example, we save the above content in a file named db.ini, then use the read() method to parse and read, and finally use the items() method to obtain all 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 presents the value in the form of a string by default, so this is why we do not add quotation marks in the db.ini file but directly write the literal above.
After obtaining the key-value pair, I actually converted it directly into a dictionary, and then unpacked the parameters to 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
## The #json format can be said to be a common file format for us, and it is also a popular data exchange format on the Internet. In addition, json is sometimes also a type of configuration file. For example, npm (a JavaScript package management tool similar to Python's pip) and the widely used VSCode editor produced by Microsoft all use json to write configuration parameters. Like configparser, Python also has a built-in json standard library, which can import file and string json content through the load() and loads() methods.{ "localdb":{ "host": "127.0.0.1", "user": "root", "password": "123456", "port": 3306, "database": "mysql" } }We save the above content as db.json and then read and parse it. It is relatively simple and easy for the json library to read json files, and it is easy to parse into Python dictionary objects.
>>> 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'}The disadvantage of using json file configuration is that the syntax standards are strictly limited. One of the criticisms is that comments cannot be written in it, unless other supersets of the json type are used as alternatives (comments can be written in VSCode json parameter configuration file is one alternative); at the same time, there is the problem of too deep nesting, which can easily lead to errors. It should not be used to write long or complex parameter configuration information. toml The toml format (or tml format) is a configuration file format proposed by Github co-founder Tom Preston-Werner. According to Wikipedia, toml was first proposed in July 2013, seven years ago; it is also somewhat similar to the yaml file to be discussed later in some aspects, but if you know the yaml When the specification has dozens of pages (yes, it's really dozens of pages...), you may not really be willing to write such a complicated configuration file, so the toml format is a good choice. The format of toml is roughly as follows:
pip install tomlThe parsing method of this library is very simple, and it is somewhat similar to the parsing usage of the json library, that is, through load() or loads() for parsing; similarly, conversion and export are also used similarly. For example, we now write the following content into config.toml:
[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"]
緊接著我們就可以通過 toml 庫中的 load() 方法來進行讀?。?/p>
>>> 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'}}
可以看到 toml 文件被間接地轉化成了字典類型,當然這也就是 json 版的寫法(將單引號替換成雙引號即可),方便我們后續(xù)調用或者傳參。
yaml
yaml 格式(或 yml 格式)是目前較為流行的一種配置文件,它早在 2001 由一個名為 Clark Evans 的人提出;同時它也是目前被廣泛使用的配置文件類型,典型的就是 Docker 容器里的 docker-compose.yml 配置文件,如果經(jīng)常使用 Docker 進行部署的人對此不會陌生。
yaml 文件的設計從 Python、XML 等地方獲取靈感,所以在使用時能很清楚地看到這些部分的影子。
在上一節(jié) toml 內容里我曾提到,yaml 的規(guī)范內容可以說是冗長和復雜,足足有80頁之多(斗尊強者,恐怖如斯……)。
02-yaml規(guī)范頁數(shù)
所以感興趣的朋友可以再自行了解相關用法。
YAML 官方早已經(jīng)提供了相應的 Python 庫進行支持,即 PyYAML;當然也同樣需要我們事先進行安裝:
pip install pyyaml
同 json 庫和 toml 庫一樣,通過 load() 方法來進行加載。
需要注意的是,使用 load() 方法會存在一定的安全隱患,從思科 Talos 的這份報告中我們可以看到,如果加載了未知或不信任的 yaml 文件,那么有可能會存在被攻擊的風險和網(wǎng)絡安全隱患,因為它能夠直接調用相應的 Python 函數(shù)來執(zhí)行為攻擊者所需要的命令,比如說在 yaml 文件中寫入這么一段:
# 使用Linux和macOS的朋友不要輕易嘗試 !!python/object/apply:os.system ["rm -rf /"]
因此最好是使用 safe_load() 來代替 load() 方法。
這和 Python 內置的 string 標準庫中 Template 類的 substitute() 模板方法一樣存在著同樣的安全隱患,所以使用 safe_substitute() 來替代是一樣的道理。
如我們現(xiàn)在將之前的一些配置信息寫入 config.yaml 文件中:
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
然后我們通過 safe_load() 方法進行解析:
>>> 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'}}
可以看到最后結果和前面的 toml 庫的解析結果基本一致。
結尾
本文列舉了一些主流且常見的配置文件類型及其 Python 的讀取方法,可能有的讀者會發(fā)現(xiàn)當中沒有 xml 格式類型的內容。對于 xml 配置文件可能與 Java 系語言打交道的朋友遇見得會多一些,但 xml 文件的可讀性實在是讓人望而生畏;對 xml 文件不了解的朋友可以使用 Chrome 瀏覽器隨便進入一個網(wǎng)站然后按下 F12 進入開發(fā)者后查看那密密麻麻的 html 元素便是 .xml 的縮影。
除了這些主流的配置文件類型之外,像一些 .cfg、.properties 等都可以作為配置文件,甚至和開頭提到的那樣,你單獨用一個 .py 文件來書寫各類配置信息作為配置文件進行導入都是沒問題,只是在跨語言共享時可能會有些障礙。因此本文就不過多介紹,感興趣的朋友可以進一步自行了解。
在本文里列舉的配置文件類型其復雜性由上到下依次增加:ini
The above is the detailed content of Super complete! Common ways to write configuration files in Python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

When choosing a suitable PHP framework, you need to consider comprehensively according to project needs: Laravel is suitable for rapid development and provides EloquentORM and Blade template engines, which are convenient for database operation and dynamic form rendering; Symfony is more flexible and suitable for complex systems; CodeIgniter is lightweight and suitable for simple applications with high performance requirements. 2. To ensure the accuracy of AI models, we need to start with high-quality data training, reasonable selection of evaluation indicators (such as accuracy, recall, F1 value), regular performance evaluation and model tuning, and ensure code quality through unit testing and integration testing, while continuously monitoring the input data to prevent data drift. 3. Many measures are required to protect user privacy: encrypt and store sensitive data (such as AES

Use Seaborn's jointplot to quickly visualize the relationship and distribution between two variables; 2. The basic scatter plot is implemented by sns.jointplot(data=tips,x="total_bill",y="tip",kind="scatter"), the center is a scatter plot, and the histogram is displayed on the upper and lower and right sides; 3. Add regression lines and density information to a kind="reg", and combine marginal_kws to set the edge plot style; 4. When the data volume is large, it is recommended to use "hex"

The core idea of PHP combining AI for video content analysis is to let PHP serve as the backend "glue", first upload video to cloud storage, and then call AI services (such as Google CloudVideoAI, etc.) for asynchronous analysis; 2. PHP parses the JSON results, extract people, objects, scenes, voice and other information to generate intelligent tags and store them in the database; 3. The advantage is to use PHP's mature web ecosystem to quickly integrate AI capabilities, which is suitable for projects with existing PHP systems to efficiently implement; 4. Common challenges include large file processing (directly transmitted to cloud storage with pre-signed URLs), asynchronous tasks (introducing message queues), cost control (on-demand analysis, budget monitoring) and result optimization (label standardization); 5. Smart tags significantly improve visual

To integrate AI sentiment computing technology into PHP applications, the core is to use cloud services AIAPI (such as Google, AWS, and Azure) for sentiment analysis, send text through HTTP requests and parse returned JSON results, and store emotional data into the database, thereby realizing automated processing and data insights of user feedback. The specific steps include: 1. Select a suitable AI sentiment analysis API, considering accuracy, cost, language support and integration complexity; 2. Use Guzzle or curl to send requests, store sentiment scores, labels, and intensity information; 3. Build a visual dashboard to support priority sorting, trend analysis, product iteration direction and user segmentation; 4. Respond to technical challenges, such as API call restrictions and numbers

The core of PHP's development of AI text summary is to call external AI service APIs (such as OpenAI, HuggingFace) as a coordinator to realize text preprocessing, API requests, response analysis and result display; 2. The limitation is that the computing performance is weak and the AI ecosystem is weak. The response strategy is to leverage APIs, service decoupling and asynchronous processing; 3. Model selection needs to weigh summary quality, cost, delay, concurrency, data privacy, and abstract models such as GPT or BART/T5 are recommended; 4. Performance optimization includes cache, asynchronous queues, batch processing and nearby area selection. Error processing needs to cover current limit retry, network timeout, key security, input verification and logging to ensure the stable and efficient operation of the system.

String lists can be merged with join() method, such as ''.join(words) to get "HelloworldfromPython"; 2. Number lists must be converted to strings with map(str, numbers) or [str(x)forxinnumbers] before joining; 3. Any type list can be directly converted to strings with brackets and quotes, suitable for debugging; 4. Custom formats can be implemented by generator expressions combined with join(), such as '|'.join(f"[{item}]"foriteminitems) output"[a]|[
