strictyaml 1.7.3


pip install strictyaml

  Latest version

Released: Mar 10, 2023


Meta
Author: Colm O'Connor
Requires Python: >=3.7.0

Classifiers

Programming Language
  • Python :: 3

License
  • OSI Approved :: MIT License

Topic
  • Text Processing :: Markup
  • Software Development :: Libraries

Natural Language
  • English

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 Mar 10, 2023
1.7.0 Mar 01, 2023
1.6.2 Oct 08, 2022
1.6.1 Nov 28, 2021
1.6.0 Nov 20, 2021
1.5.0 Oct 14, 2021
1.4.4 Jun 19, 2021
1.4.2 May 31, 2021
1.4.1 May 31, 2021
1.4.0 Mar 28, 2021
1.3.2 Jan 18, 2021
1.3.1 Jan 12, 2021
1.3.0 Jan 10, 2021
1.2.0 Jan 02, 2021
1.1.1 Nov 14, 2020
1.1.0 Aug 12, 2020
1.0.7 Aug 05, 2020
1.0.6 Nov 02, 2019
1.0.5 Oct 05, 2019
1.0.4 Sep 22, 2019
1.0.3 Aug 02, 2019
1.0.2 Jun 26, 2019
1.0.1 Apr 06, 2019
1.0.0 Dec 04, 2018
0.15.4 Nov 15, 2018
0.15.3 Nov 05, 2018
0.15.2 Nov 05, 2018
0.15.1 Nov 03, 2018
0.15.0 Oct 29, 2018
0.14.1 Oct 20, 2018
0.13.0 Sep 18, 2018
0.12.0 Sep 18, 2018
0.11.10 Aug 04, 2018
0.11.9 Jun 30, 2018
0.11.8 Apr 14, 2018
0.11.7 Jan 21, 2018
0.11.6 Jan 20, 2018
0.11.5 Jan 17, 2018
0.11.4 Jan 04, 2018
0.11.3 Jan 02, 2018
0.11.2 Jan 02, 2018
0.11.1 Jan 01, 2018
0.11.0 Dec 07, 2017
0.10.0 Nov 09, 2017
0.9.0 Oct 31, 2017
0.8.0 Oct 24, 2017
0.7.3 Oct 05, 2017
0.7.2 Sep 12, 2017
0.7.1 Aug 21, 2017
0.7.0 Jul 03, 2017
0.6.2 Jun 24, 2017
0.6.1 Jun 07, 2017
0.6.0 Jun 06, 2017
0.5.9 May 29, 2017
0.5.8 May 25, 2017
0.5.7 May 21, 2017
0.5.6 May 14, 2017
0.5.5 Apr 12, 2017
0.5.4 Apr 10, 2017
0.5.3 Apr 07, 2017
0.5.2 Mar 26, 2017
0.5.1 Mar 11, 2017
0.5.0 Feb 24, 2017
0.4.2 Feb 04, 2017
0.4.1 Dec 10, 2016
0.4.0 Dec 07, 2016
0.3.9 Nov 27, 2016
0.3.8 Nov 26, 2016
0.3.7 Nov 26, 2016
0.3.6 Nov 25, 2016
0.3.5 Nov 05, 2016
0.3.3 Nov 01, 2016
0.3.2 Oct 30, 2016
0.3.1 Oct 30, 2016
0.3 Oct 23, 2016
0.2 Oct 17, 2016
0.1.6 Sep 11, 2016
0.1.5 Sep 11, 2016
0.1.4 Aug 29, 2016
0.1.3 Aug 17, 2016
0.1.2 Aug 14, 2016
0.1.1 Jul 29, 2016
0.1 Jun 19, 2016

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras: None
Dependencies:
python-dateutil (>=2.6.0)