Skip to main content

StrictYAML

StrictYAML is a type-safe YAML parser that parses a restricted subset of the YAML specificaton.

Priorities:

  • Readability of YAML.

  • Ease of use of API.

  • Secure by default.

  • Strict validation of markup and straightforward type casting.

  • Clear, readable exceptions with line numbers.

  • Acting as a near-drop in replacement for pyyaml, ruamel.yaml or poyo.

  • Ability to read in (commented) YAML and write it out again with comments preserved.

  • Letting you worry about more interesting things than parsing or writing config files.

Simple example:

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

Default parse result:

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

>>> strictyaml.load(yaml_str).data
{"name": "Ford Prefect", "age": "42", "possessions": ["Towel", ]}   # All data is str, list or dict

Using a schema:

>>> from strictyaml import load, Map, Str, Int, Seq, YAMLError
>>> schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())})
>>> person = load(yaml_str, schema)
>>> person.data == {"name": "Ford Prefect", "age": 42, "possessions": ["Towel", ]}     # 42 is now an int

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
>>> try:
...     person = load(yaml_str, 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 you can modify values and write out the YAML with comments preserved:

>>> 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['possessions'][0].start_line
5

All other features are documented using the example driven documentation.

Install It

$ pip install strictyaml

FAQ

From learning programmers:

If you’re looking at this and thinking “why not do/use X instead?” that’s a healthy response, and you deserve answers. These are probably the questions you’re asking:

Breaking changes

0.5: Data is now parsed by default as a YAML object instead of directly to dict/list. To get dict/list and ordinary values as before, get yaml_object.data.

0.7: Roundtripping now requires that you only assign YAML objects to index: e.g. yaml_object[‘x’] = another_yaml_obj

Contributors

  • @gvx

  • @AlexandreDecan

  • @lots0logs

  • @tobbez

Metadata

Release files for strictyaml 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strictyaml 0.9.0
File Size Uploaded Provenance Actions
strictyaml-0.9.0.tar.gz 16.0 kB Without provenance Details for strictyaml-0.9.0.tar.gz

Release files / strictyaml-0.9.0.tar.gz

Download URL strictyaml-0.9.0.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
392692646864b4efd4203721839b1c06a9c08f59bdd46dd0d3456d804c19f938
BLAKE2b-256 checksum
How to use checksums
015fd1330fbf8c1b435af0d46877d5ca5d28b844c7fd298fe87d7b53e88cc88d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for strictyaml 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page