想象一下您的项目中有一个这样的文件:
# 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中文网其他相关文章!

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

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

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

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

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

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

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

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


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

记事本++7.3.1
好用且免费的代码编辑器

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。