严格的、类型化的yaml解析器
strictyaml的Python项目详细描述
严格
strictyaml是一个type-safeyaml解析器 解析并验证YAML的restricted subset 规范。
优先级:
- 漂亮的API
- 拒绝像the Norway problem那样分析the ugly, hard to read and insecure features of YAML。
- 严格的标记验证和简单的类型转换。
- 清除代码段和行号的可读异常。
- 作为一个接近下降的替代品,派雅姆,鲁亚姆或波约。
- 能够读入yaml,进行修改,并在保留注释的情况下再次写出。
- Not speed,目前。
简单示例:
# All about the charactername:Ford Prefectage:42possessions:-Towel
fromstrictyamlimportload,Map,Str,Int,Seq,YAMLError
默认分析结果:
>>>load(yaml_snippet)YAML(OrderedDict([('name','Ford Prefect'),('age','42'),('possessions',['Towel'])]))
所有数据都是字符串、列表或顺序数据:
>>>load(yaml_snippet).dataOrderedDict([('name','Ford Prefect'),('age','42'),('possessions',['Towel'])])
快速启动模式:
fromstrictyamlimportload,Map,Str,Int,Seq,YAMLErrorschema=Map({"name":Str(),"age":Int(),"possessions":Seq(Str())})
42现在被解析为一个整数:
>>>person=load(yaml_snippet,schema)>>>person.dataOrderedDict([('name','Ford Prefect'),('age',42),('possessions',['Towel'])])
如果存在语法问题、违反模式或使用不允许的yaml功能,则会引发yamlerror:
# All about the charactername:Ford Prefectage:42
例如,架构冲突:
try:person=load(yaml_snippet,schema)exceptYAMLErroraserror:print(error)
while parsing a mappingin "<unicode string>", line 1, column 1:# All about the character^ (line:1)required key(s) 'possessions' not foundin "<unicode string>", line 3, column 1:age:'42'^ (line:3)
如果分析正确:
fromstrictyamlimportload,Map,Str,Int,Seq,YAMLErrorschema=Map({"name":Str(),"age":Int(),"possessions":Seq(Str())})
您可以修改值并写出保留注释的yaml:
person=load(yaml_snippet,schema)person['age']=43print(person.as_yaml())
# All about the charactername:Ford Prefectage:43possessions:-Towel
以及查找行号:
>>>person=load(yaml_snippet,schema)>>>person['possessions'][0].start_line5
安装
$ pip install strictyaml
为什么是严格的?
有许多格式和方法可以实现 与Strictymal的用途不同。我试着把它变成最好的。 以下是一系列书面证明:
- Why not JSON for simple configuration files?
- What is wrong with TOML?
- Why not use the YAML 2.0 standard? - we don't need a new standard!
- Why not use kwalify with standard YAML to validate my YAML?
- Why not use python's schema library (or similar) for validation?
- Why not HOCON?
- Why not JSON5?
- Why not use XML for configuration or DSLs?
- Why shouldn't I just use python code for configuration?
- Why not use INI files?
- Why not use SDLang?
- Why avoid using environment variables as configuration?
- Why not use JSON Schema for validation?
使用strictyaml
如何:
- Get line numbers of YAML elements
- Revalidate an already validated document
- Build a YAML document from scratch in code
- Reading in YAML, editing it and writing it back out
- Labeling exceptions
- Merge YAML documents
- Either/or schema validation of different, equally valid different kinds of YAML
- Parsing YAML without a schema
复合验证器:
- Using a YAML object of a parsed mapping
- Sequence/list validator (Seq)
- Sequences of unique items (UniqueSeq)
- Mapping with defined keys and a custom key validator (Map)
- Fixed length sequences (FixedSeq)
- Mappings with arbitrary key names (MapPattern)
- Optional keys with defaults (Map/Optional)
- Mappings with defined keys (Map)
- Validating optional keys in mappings (Map)
标量验证器:
- Validating strings with regexes (Regex)
- Parsing comma separated items (CommaSeparated)
- Datetimes (Datetime)
- Empty key validation
- Decimal numbers (Decimal)
- Boolean (Bool)
- Integers (Int)
- Enumerated scalars (Enum)
- Floating point numbers (Float)
- Email and URL validators
- Parsing strings (Str)
限制:
设计理由
在Strictymal中有一些设计决策是有争议的 和/或不明显。这些记录如下:
- What is wrong with explicit tags?
- What is syntax typing?
- The Norway Problem - why StrictYAML refuses to do implicit typing and so should you
- What is wrong with flow style YAML?
- Why does StrictYAML only parse from strings and not files?
- What is wrong with duplicate keys?
- Why is parsing speed not a high priority for StrictYAML?
- Why does StrictYAML not parse direct representations of python objects?
- What is wrong with node anchors and references?
- Why does StrictYAML make you define a schema in python - a turing complete language?
贡献者
- @gvx
- @亚历山大
- @LOTS0日志
- @托比兹
贡献
在编写任何代码之前,请阅读contributing to hitchdev libraries上的教程。
在编写任何代码之前,如果您提出了一个新功能,请在github上提出。如果它是一个现有的特征/ bug,请评论并简要描述您将如何实现它。
- 所有的代码都需要伴随着一个故事来练习或修改现有的故事。这用于测试代码和构建文档。