Skip to main content

Strict, typed YAML parser

Project description

StrictYAML is a type-safe YAML parser, eliminating surprise bugs caused by implicit typing.

For example:

- Don Corleone: Do you have faith in my judgment?
- Clemenza: Yes
- Don Corleone: Do I have your loyalty?

Parse output of pyyaml and ruamel.yaml:

>>> from ruamel.yaml import load
>>> load(the_godfather)
[{'Don Corleone': 'Do you have faith in my judgement?'},
 {'Clemenza': True},
 {'Don Corleone': 'Do I have your loyalty?'},]

Wait, Clemenza said what??

Parse output of StrictYAML:

>>> from strictyaml import load, List, MapPattern, Str
>>> load(the_godfather, List(MapPattern({Str(), Str()})))
[{'Don Corleone': 'Do you have faith in my judgement?'},
 {'Clemenza': 'Yes'},
 {'Don Corleone': 'Do I have your loyalty?'},]

Why use StrictYAML?

YAML is an excellent metalanguage to describe most kinds of configuration as well as for creating simple DSLs.

It is terse and devoid of noisy syntax (unlike JSON), able to clearly represent hierarchical data (unlike INI) and most important of all, it’s not the product of a crazed committee of monkeys hammering away on typewriters (XML).

YAML has weaknesses, however, which StrictYAML addresses:

  • Implicitly typed (StrictYAML fixes this with explicit typing).

  • Allows arbitrary binary data leading to, among other things, Ruby on Rails’ spectacular security fail (disallowed in StrictYAML because why on earth did anybody think this was a good idea?).

  • Tag tokens, allowing you to specify types in the YAML (disallowed in StrictYAML).

  • Confusing “flow” style (disallowed by default in StrictYAML).

  • Often confusing node anchors and references (disallowed by default in StrictYAML).

Using StrictYAML

Map pattern in YAML can be translated to dicts:

>>> from strictyaml import load, MapPattern, Str, Int
>>> load("a: 1\nb: 2", MapPattern(Str(), Int()))
{'a': 1, 'b': 2}

Restricted, specified maps can also be translated to dicts:

>>> from strictyaml import load, Map, Bool
>>> load("a: yes\nb: no", Map({"a": Bool(), "b": Bool()}))
{'a': True, 'b': False}

Validators can be nested and lists can be restricted to a single type:

>>> from strictyaml import load, Map, Seq, Float
>>> list_float_typed_yaml = """
... a:
...   - 1.5
...   - 2.5
... b:
...   - 2
...   - -3.14e5
... """
>>> load(list_float_typed_yaml, Map({"a": Seq(Float()), "b": Seq(Float())}))
{'b': [2.0, -314000.0], 'a': [1.5, 2.5]}

Types can be mixed and matched:

>>> from strictyaml import load, Map, Bool, Str, Decimal
>>> product_yaml = """
... Name: Tea
... Price: 3.55
... Available: Yes
... """
>>> load(product_yaml, Map({"Name": Str(), "Price": Decimal(), "Available": Bool()}))
{'Available': True, 'Name': 'Tea', 'Price': Decimal('3.55')}

You can also use enums:

>>> from strictyaml import load, Map, Str, Enum
>>> product_yaml = """
... Name: Tea
... Type: Green
... """
>>> load(product_yaml, Map({"Name": Str(), "Type": Enum(["Green", "Mint"])}))
{'Name': 'Tea', 'Type': 'Green'}

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strictyaml-0.1.3.tar.gz (6.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page