strictyaml

Strict, typed YAML parser

MIT 83 个版本 Python >=3.7.0
安装
pip install strictyaml
poetry add strictyaml
pipenv install strictyaml
conda install strictyaml
描述

StrictYAML

StrictYAML is a type-safe YAML parser that parses and validates a restricted subset of the YAML specification.

Priorities:

  • Beautiful API
  • Refusing to parse the ugly, hard to read and insecure features of YAML like the Norway problem.
  • Strict validation of markup and straightforward type casting.
  • Clear, readable exceptions with code snippets and line numbers.
  • Acting as a near-drop in replacement for pyyaml, ruamel.yaml or poyo.
  • Ability to read in YAML, make changes and write it out again with comments preserved.
  • Not speed, currently.

Simple example:

# All about the character
name: Ford Prefect
age: 42
possessions:
- Towel

from strictyaml import load, Map, Str, Int, Seq, YAMLError

Default parse result:

>>> load(yaml_snippet)
YAML({'name': 'Ford Prefect', 'age': '42', 'possessions': ['Towel']})

All data is string, list or OrderedDict:

>>> load(yaml_snippet).data
{'name': 'Ford Prefect', 'age': '42', 'possessions': ['Towel']}

Quickstart with schema:

from strictyaml import load, Map, Str, Int, Seq, YAMLError

schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())})

42 is now parsed as an integer:

>>> person = load(yaml_snippet, schema)
>>> person.data
{'name': 'Ford Prefect', 'age': 42, 'possessions': ['Towel']}

A YAMLError will be raised if there are syntactic problems, violations of your schema or use of disallowed YAML features:

# All about the character
name: Ford Prefect
age: 42

For example, a schema violation:

try:
    person = load(yaml_snippet, schema)
except YAMLError as error:
    print(error)

while parsing a mapping
  in "<unicode string>", line 1, column 1:
    # All about the character
     ^ (line: 1)
required key(s) 'possessions' not found
  in "<unicode string>", line 3, column 1:
    age: '42'
    ^ (line: 3)

If parsed correctly:

from strictyaml import load, Map, Str, Int, Seq, YAMLError, as_document

schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())})

You can modify values and write out the YAML with comments preserved:

person = load(yaml_snippet, schema)
person['age'] = 43
print(person.as_yaml())

# All about the character
name: Ford Prefect
age: 43
possessions:
- Towel

As well as look up line numbers:

>>> person = load(yaml_snippet, schema)
>>> person['possessions'][0].start_line
5

And construct YAML documents from dicts or lists:

print(as_document({"x": 1}).as_yaml())

x: 1

Install

$ pip install strictyaml

Why StrictYAML?

There are a number of formats and approaches that can achieve more or less the same purpose as StrictYAML. I've tried to make it the best one. Below is a series of documented justifications:

Using StrictYAML

How to:

Compound validators:

Scalar validators:

Restrictions:

Design justifications

There are some design decisions in StrictYAML which are controversial and/or not obvious. Those are documented here:

Star Contributors

  • @wwoods
  • @chrisburr
  • @jnichols0

Other Contributors

  • @eulores
  • @WaltWoods
  • @ChristopherGS
  • @gvx
  • @AlexandreDecan
  • @lots0logs
  • @tobbez
  • @jaredsampson
  • @BoboTIG

StrictYAML also includes code from ruamel.yaml, Copyright Anthon van der Neut.

Contributing

  • Before writing any code, please read the tutorial on contributing to hitchdev libraries.
  • Before writing any code, if you're proposing a new feature, please raise it on github. If it's an existing feature / bug, please comment and briefly describe how you're going to implement it.
  • All code needs to come accompanied with a story that exercises it or a modification to an existing story. This is used both to test the code and build the documentation.
版本列表
1.7.3 2023-03-10
1.7.0 2023-03-01
1.6.2 2022-10-08
1.6.1 2021-11-28
1.6.0 2021-11-20
1.5.0 2021-10-14
1.4.4 2021-06-19
1.4.2 2021-05-31
1.4.1 2021-05-31
1.4.0 2021-03-28
1.3.2 2021-01-18
1.3.1 2021-01-12
1.3.0 2021-01-10
1.2.0 2021-01-02
1.1.1 2020-11-14
1.1.0 2020-08-12
1.0.7 2020-08-05
1.0.6 2019-11-02
1.0.5 2019-10-05
1.0.4 2019-09-22
1.0.3 2019-08-02
1.0.2 2019-06-26
1.0.1 2019-04-06
1.0.0 2018-12-04
0.15.4 2018-11-15
0.15.3 2018-11-05
0.15.2 2018-11-05
0.15.1 2018-11-03
0.15.0 2018-10-29
0.14.1 2018-10-20
0.13.0 2018-09-18
0.12.0 2018-09-18
0.11.10 2018-08-04
0.11.9 2018-06-30
0.11.8 2018-04-14
0.11.7 2018-01-21
0.11.6 2018-01-20
0.11.5 2018-01-17
0.11.4 2018-01-04
0.11.3 2018-01-02
0.11.2 2018-01-02
0.11.1 2018-01-01
0.11.0 2017-12-07
0.10.0 2017-11-09
0.9.0 2017-10-31
0.8.0 2017-10-24
0.7.3 2017-10-05
0.7.2 2017-09-12
0.7.1 2017-08-21
0.7.0 2017-07-03
0.6.2 2017-06-24
0.6.1 2017-06-07
0.6.0 2017-06-06
0.5.9 2017-05-29
0.5.8 2017-05-25
0.5.7 2017-05-21
0.5.6 2017-05-14
0.5.5 2017-04-12
0.5.4 2017-04-10
0.5.3 2017-04-07
0.5.2 2017-03-26
0.5.1 2017-03-11
0.5.0 2017-02-24
0.4.2 2017-02-04
0.4.1 2016-12-10
0.4.0 2016-12-07
0.3.9 2016-11-27
0.3.8 2016-11-26
0.3.7 2016-11-26
0.3.6 2016-11-25
0.3.5 2016-11-05
0.3.3 2016-11-01
0.3.2 2016-10-30
0.3.1 2016-10-30
0.3 2016-10-23
0.2 2016-10-17
0.1.6 2016-09-11
0.1.5 2016-09-11
0.1.4 2016-08-29
0.1.3 2016-08-17
0.1.2 2016-08-14
0.1.1 2016-07-29
0.1 2016-06-19