搜索
首页后端开发Python教程pyya - 在 Python 项目中管理 YAML 配置的方法

pyya - The way to manage YAML config in your Python project

想象一下您的项目中有一个这样的文件:

# config.yaml
database:   
    host: localhost
    port: 5432
    username: postgres
    password: postgres

要在 Python 中解析这个 dot yaml 配置文件,你通常会这样做:

import yaml # pip install PyYAML

with open("config.yaml", encoding='utf-8') as fstream:
    config_yaml = yaml.safe_load(fstream)

要从生成的字典创建类似 Javascript 的对象,您可以添加以下内容:

from munch import munchify # pip install munch

config = munchify(config_yaml)

pyya 使用一个函数自动执行此操作:

from pyya import init_config

config = init_config(
    'config.yaml', 'default.config.yaml', 
    merge_configs = False,
    convert_keys_to_snake_case = False,
    add_underscore_prefix_to_keywords = False
    raise_error_non_identifiers = False)
print(config.database)

# Output:
# Munch({"host": "localhost", "port": 5432, "username": "postgres", "password": "postgres"})

所有这些标志怎么样?

merge_configs 如果设置为 True,会将 config.yaml 与 default.config.yaml(或您指定的任何名称或路径)进行比较,并将后者中不存在的所有字段添加到前者中。如果设置为 False,则禁用其余标志完成的所有合并和所有格式化。

convert_keys_to_snake_case 有点不言自明,因为它只是让配置键看起来像Python。但是,此标志可能会破坏某些设置,例如日志记录配置。

add_underscore_prefix_to_keywords 如果配置键也是 Python 关键字,则会在其前面添加下划线(class 变为 _class)。它可以让属性访问工作得更好。

raise_error_non_identifiers 如果配置键也是无效的 Python 标识符,则会引发错误。

你可以像这样安装 pyya:

pip install pyya

欢迎贡献和建议:https://github.com/shadowy-pycoder/pyya

以上是pyya - 在 Python 项目中管理 YAML 配置的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

对于循环和python中的循环时:每个循环的优点是什么?对于循环和python中的循环时:每个循环的优点是什么?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。