| 1 |
Simple example: |
| 2 |
based on: strictyaml |
| 3 |
given: |
| 4 |
yaml_snippet: | |
| 5 |
# All about the character |
| 6 |
name: Ford Prefect |
| 7 |
age: 42 |
| 8 |
possessions: |
| 9 |
- Towel |
| 10 |
setup: | |
| 11 |
from strictyaml import load, Map, Str, Int, Seq, YAMLError |
| 12 |
variations: |
| 13 |
Default parse result: |
| 14 |
fails on python 2: yes |
| 15 |
steps: |
| 16 |
- Run: |
| 17 |
code: | |
| 18 |
load(yaml_snippet) |
| 19 |
will output: |- |
| 20 |
YAML({'name': 'Ford Prefect', 'age': '42', 'possessions': ['Towel']}) |
| 21 |
in interpreter: yes |
| 22 |
|
| 23 |
All data is string, list or OrderedDict: |
| 24 |
fails on python 2: yes |
| 25 |
steps: |
| 26 |
- Run: |
| 27 |
code: | |
| 28 |
load(yaml_snippet).data |
| 29 |
will output: |- |
| 30 |
{'name': 'Ford Prefect', 'age': '42', 'possessions': ['Towel']} |
| 31 |
in interpreter: yes |
| 32 |
|
| 33 |
Quickstart with schema: |
| 34 |
based on: simple example |
| 35 |
given: |
| 36 |
setup: | |
| 37 |
from strictyaml import load, Map, Str, Int, Seq, YAMLError |
| 38 |
|
| 39 |
schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())}) |
| 40 |
variations: |
| 41 |
42 is now parsed as an integer: |
| 42 |
steps: |
| 43 |
- Run: |
| 44 |
in interpreter: yes |
| 45 |
code: | |
| 46 |
person = load(yaml_snippet, schema) |
| 47 |
person.data |
| 48 |
will output: |- |
| 49 |
{'name': 'Ford Prefect', 'age': 42, 'possessions': ['Towel']} |
| 50 |
|
| 51 |
A YAMLError will be raised if there are syntactic problems, violations of your schema or use of disallowed YAML features: |
| 52 |
based on: quickstart with schema |
| 53 |
given: |
| 54 |
yaml_snippet: | |
| 55 |
# All about the character |
| 56 |
name: Ford Prefect |
| 57 |
age: 42 |
| 58 |
variations: |
| 59 |
For example, a schema violation: |
| 60 |
steps: |
| 61 |
- Run: |
| 62 |
code: | |
| 63 |
try: |
| 64 |
person = load(yaml_snippet, schema) |
| 65 |
except YAMLError as error: |
| 66 |
print(error) |
| 67 |
will output: |- |
| 68 |
while parsing a mapping |
| 69 |
in "<unicode string>", line 1, column 1: |
| 70 |
# All about the character |
| 71 |
^ (line: 1) |
| 72 |
required key(s) 'possessions' not found |
| 73 |
in "<unicode string>", line 3, column 1: |
| 74 |
age: '42' |
| 75 |
^ (line: 3) |
| 76 |
|
| 77 |
If parsed correctly: |
| 78 |
based on: simple example |
| 79 |
given: |
| 80 |
setup: | |
| 81 |
from strictyaml import load, Map, Str, Int, Seq, YAMLError, as_document |
| 82 |
|
| 83 |
schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())}) |
| 84 |
variations: |
| 85 |
You can modify values and write out the YAML with comments preserved: |
| 86 |
steps: |
| 87 |
- Run: |
| 88 |
code: | |
| 89 |
person = load(yaml_snippet, schema) |
| 90 |
person['age'] = 43 |
| 91 |
print(person.as_yaml()) |
| 92 |
will output: |- |
| 93 |
# All about the character |
| 94 |
name: Ford Prefect |
| 95 |
age: 43 |
| 96 |
possessions: |
| 97 |
- Towel |
| 98 |
|
| 99 |
|
| 100 |
As well as look up line numbers: |
| 101 |
steps: |
| 102 |
- Run: |
| 103 |
in interpreter: yes |
| 104 |
code: | |
| 105 |
person = load(yaml_snippet, schema) |
| 106 |
person['possessions'][0].start_line |
| 107 |
will output: 5 |
| 108 |
|
| 109 |
And construct YAML documents from dicts or lists: |
| 110 |
steps: |
| 111 |
- Run: |
| 112 |
in interpreter: no |
| 113 |
code: | |
| 114 |
print(as_document({"x": 1}).as_yaml()) |
| 115 |
will output: |- |
| 116 |
x: 1 |