Strict, typed YAML parser
Project description
StrictYAML is a type-safe YAML parser, built on ruamel.yaml that eliminates most of the bullshit in the YAML spec.
It’s designed to serve as an easy drop in replacement parser for either pyyaml, ruamel.yaml or poyo.
Simple example:
name: Ford Prefect
age: 42
posessions:
- Towel
>>> strictyaml.load(yaml) \
== {"name": "Ford Prefect", "age": "42", "possessions": ["Towel", ]} # note 42 is a string
Example using validator with mapping, sequence, string and integer:
>>> from strictyaml import load, Map, Str, Int, Seq
>>> load(yaml, Map({"name": Str(), "age": Int(), "possessions": Seq(Str())})) \
== {"name": "Ford Prefect", "age": 42, "possessions": ["Towel", ]} # 42 is now an int
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:
Map Patterns
If you’re not sure what the key is going to be but you know what type the values will be, use this.
emails:
arthur: arthur@earth.gov
zaphod: zaphod@beeblebrox.com
ford: ford@ursa-minor.com
>>> from strictyaml import load, Map, MapPattern, Str
>>> load(yaml, Map({"emails": MapPattern({Str(), Str()})}) \
== {"emails": {"arthur": "arthur@earth.gov", "zaphod": "zaphod@beeblebrox.com", "ford": "ford@ursa-minor.com"}}
All Scalar Types
StrictYAML will parse a string into the correct data type if you specify it:
>>> import from strictyaml import load, Map
>>> load("string: string", Map({"string": strictyaml.Str()})) == {"string": "string"}
>>> load("float: 42.3333", Map({"float": strictyaml.Float()})) == {"string": 42.3333}
>>> load("price: 35.42811", Map({"price": strictyaml.Decimal()})) == {"price": decimal.Decimal('35.32811')}
>>> load("booltrue: yes", Map({"booltrue": strictyaml.Bool()})) == {"booltrue": True}
>>> load("boolfalse: no", Map({"boolfalse": strictyaml.Bool()})) == {"booltrue": True}
>>> load("booltrue: true", Map({"booltrue": strictyaml.Bool()})) == {"booltrue": True}
>>> load("boolfalse: False", Map({"boolfalse": strictyaml.Bool()})) == {"booltrue": False}
>>> load("enum: monday", Map({"enum": strictyaml.Enum(["monday", "tuesday", "wednesday"])})) == {"enum": "monday"}
Custom scalar types
COMING SOON
Using Kwalify
See: What is kwalify and when should I use it?
COMING SOON
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.