python的轻量级yaml解析器。γ
poyo的Python项目详细描述
POYO
python的轻量级yaml解析器。
poyo不允许反序列化任意python对象。支持
类型有str
、bool
、int
、float
、NoneType
以及dict
。
list
值。
请注意,poyo只支持yaml格式的选定子集 这是解析cookiecutter user configuration files所必需的。poyo不支持序列化为yaml 与json不兼容。
安装
poyo在PyPI上提供,用于python 2.7及更高版本,并且可以 与pip:
一起安装pip install poyo
此软件包没有任何附加要求。
用法
poyo附带了一个parse_string()
函数,用于加载utf-8编码的字符串
数据放入python dict。
importcodecsimportloggingfrompoyoimportparse_string,PoyoExceptionlogging.basicConfig(level=logging.DEBUG)withcodecs.open("tests/foobar.yml",encoding="utf-8")asymlfile:ymlstring=ymlfile.read()try:config=parse_string(ymlstring)exceptPoyoExceptionasexc:logging.error(exc)else:logging.debug(config)
示例
输入yaml字符串
---default_context:# foobargreeting:こんにちはemail:"raphael@hackebrot.de"docs:truegui:FALSE123:456.789# comment# allthethings'some:int':1000000foo:"hallo#welt"#Inline comment :)longtext:>This is a multiline string.It can contain all manners of characters.Single line breaks are ignored,but blank linkes cause line breaks.trueish:Falseeeeeeeblog :raphael.codesrelative-root:/ # web app root path (default:'')lektor:0.0.0.0:5000# local builddoc_tools:# docs or didn't happen-mkdocs-'sphinx'-null# 今日はzZz:TrueNullValue:Null# Block# CommentHello World:# See you at EuroPythonnull:This is madness# yogh:https://github.com/{0}.git"Yay#python":Cool!
输出python dict
{u"default_context":{u"greeting":u"こんにちは",u"email":u"raphael@hackebrot.de",u"docs":True,u"gui":False,u"lektor":"0.0.0.0:5000",u"relative-root":"/",123:456.789,u"some:int":1000000,u"foo":u"hallo #welt",u"longtext":(u"This is a multiline string. It can contain all "u"manners of characters.\nSingle line breaks are "u"ignored, but blank linkes cause line breaks.\n"),u"trueish":u"Falseeeeeee",u"blog":u"raphael.codes",u"doc_tools":[u"mkdocs",u"sphinx",None],},u"zZz":True,u"NullValue":None,u"Hello World":{None:u"This is madness",u"gh":u"https://github.com/{0}.git",},u"Yay #python":u"Cool!",}
日志记录
poyo遵循logging in a library的建议,其中
意味着它不配置日志本身。它的根记录器名为poyo
所有子记录器的名称跟踪包/模块层次结构。
poyo登录到一个NullHandler
,并且仅在DEBUG
级别。
如果应用程序配置日志记录并允许显示调试消息,
使用poyo时会看到日志记录。日志消息指示哪个解析器
方法在解析器反序列化配置时用于给定字符串。你
通过设置日志级别,可以从应用程序中的poyo删除所有日志记录
将poyo
记录器的值设置为高于DEBUG
的值。
禁用日志记录
importlogginglogging.getLogger("poyo").setLevel(logging.WARNING)
调试日志配置示例
importloggingfrompoyoimportparse_stringlogging.basicConfig(level=logging.DEBUG)CONFIG="""---default_context: # foobar greeting: こんにちは gui: FALSE doc_tools: # docs or didn't happen - mkdocs - 'sphinx' 123: 456.789"""parse_string(CONFIG)
调试日志消息示例
DEBUG:poyo.parser:parse_blankline <- \n DEBUG:poyo.parser:parse_blankline -> IGNORED DEBUG:poyo.parser:parse_dashes <- ---\n DEBUG:poyo.parser:parse_dashes -> IGNORED DEBUG:poyo.parser:parse_section <- default_context: # foobar\n DEBUG:poyo.parser:parse_str <- default_context DEBUG:poyo.parser:parse_str -> default_context DEBUG:poyo.parser:parse_section -> <Section name: default_context> DEBUG:poyo.parser:parse_simple <- greeting: \u3053\u3093\u306b\u3061\u306f\n DEBUG:poyo.parser:parse_str <- greeting DEBUG:poyo.parser:parse_str -> greeting DEBUG:poyo.parser:parse_str <- \u3053\u3093\u306b\u3061\u306f DEBUG:poyo.parser:parse_str -> \u3053\u3093\u306b\u3061\u306f DEBUG:poyo.parser:parse_simple -> <Simple name: greeting, value: \u3053\u3093\u306b\u3061\u306f> DEBUG:poyo.parser:parse_simple <- gui: FALSE\n DEBUG:poyo.parser:parse_str <- gui DEBUG:poyo.parser:parse_str -> gui DEBUG:poyo.parser:parse_false <- FALSE DEBUG:poyo.parser:parse_false -> False DEBUG:poyo.parser:parse_simple -> <Simple name: gui, value: False> DEBUG:poyo.parser:parse_list <- doc_tools:\n # docs or didn't happen\n - mkdocs\n - 'sphinx'\n DEBUG:poyo.parser:parse_str <- mkdocs DEBUG:poyo.parser:parse_str -> mkdocs DEBUG:poyo.parser:parse_str <- 'sphinx' DEBUG:poyo.parser:parse_str -> sphinx DEBUG:poyo.parser:parse_str <- doc_tools DEBUG:poyo.parser:parse_str -> doc_tools DEBUG:poyo.parser:parse_list -> <Simple name: doc_tools, value: ['mkdocs', 'sphinx']> DEBUG:poyo.parser:parse_simple <- 123: 456.789\n DEBUG:poyo.parser:parse_int <- 123 DEBUG:poyo.parser:parse_int -> 123 DEBUG:poyo.parser:parse_float <- 456.789 DEBUG:poyo.parser:parse_float -> 456.789 DEBUG:poyo.parser:parse_simple -> <Simple name: 123, value: 456.789> DEBUG:poyo.parser:parse_simple <- docs: true\n DEBUG:poyo.parser:parse_str <- docs DEBUG:poyo.parser:parse_str -> docs DEBUG:poyo.parser:parse_true <- true DEBUG:poyo.parser:parse_true -> True DEBUG:poyo.parser:parse_simple -> <Simple name: docs, value: True>
关于本项目
我们创建此项目是为了解决 依赖于现有的YAML解析器的{a5}版本 对于python。有关详细信息,请查看此GitHub issue。
社区
您愿意为poyo捐款吗?你真棒!
请检查任务的good first issue标签, 这是你对波约的第一个贡献。你的 非常感谢您的贡献!每一点帮助和信任都会 总是被给予。
在poyo项目的代码库中进行交互的每个人,发布追踪器,聊天 房间和邮件列表应遵循PyPI Code of Conduct。
加入poyocommunity!
许可证
根据MIT许可证的条款发布,poyo是免费的、开源的 软件。