File: boolean.md

package info (click to toggle)
python-strictyaml 1.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,708 kB
  • sloc: python: 12,836; sh: 48; makefile: 3
file content (173 lines) | stat: -rw-r--r-- 2,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
---
title: Boolean (Bool)
---


Boolean values can be parsed using a Bool validator.

It case-insensitively interprets "yes", "true", "1", "on" as "True", "y"
and their opposites as False.

Different values will trigger a validation error.

When updating boolean values on a YAML object with True or False, the roundtripped
string version is set to "yes" and "no".

To have your boolean values updated to a different yes/no string, update
with a string instead - e.g. "on" or "off".


Example yaml_snippet:

```yaml
a: yes
b: true
c: on
d: 1
e: True
f: Y

u: n
v: False
w: 0
x: Off
y: FALSE
z: no

```


```python
from strictyaml import Bool, Str, MapPattern, load
from ensure import Ensure

schema = MapPattern(Str(), Bool())

```



Parse to YAML object:


```python
Ensure(load(yaml_snippet, schema)).equals({
    "a": True, "b": True, "c": True, "d": True, "e": True, "f": True,
    "u": False, "v": False, "w": False, "x": False, "y": False, "z": False,
})

```




YAML object should resolve to True or False:


```python
Ensure(load(yaml_snippet, schema)["w"]).equals(False)

```




Using .data you can get the actual boolean value parsed:


```python
assert load(yaml_snippet, schema)["a"].data is True

```




.text returns the text of the boolean YAML:


```python
Ensure(load(yaml_snippet, schema)["y"].text).equals("FALSE")

```




Update boolean values with string and bool type:


```python
yaml = load(yaml_snippet, schema)
yaml['a'] = 'no'
yaml['b'] = False
yaml['c'] = True
print(yaml.as_yaml())

```

```yaml
a: no
b: no
c: yes
d: 1
e: True
f: Y

u: n
v: False
w: 0
x: Off
y: FALSE
z: no
```




Cannot cast boolean to string:


```python
str(load(yaml_snippet, schema)["y"])
```


```python
builtins.TypeError:
Cannot cast 'YAML(False)' to str.
Use str(yamlobj.data) or str(yamlobj.text) instead.
```




Different uninterpretable values raise validation error:


```python
load('a: yâs', schema)

```


```python
strictyaml.exceptions.YAMLValidationError:
when expecting a boolean value (one of "yes", "true", "on", "1", "y", "no", "false", "off", "0", "n")
found arbitrary text
  in "<unicode string>", line 1, column 1:
    a: "y\xE2s"
     ^ (line: 1)
```







!!! note "Executable specification"

    Documentation automatically generated from 
    <a href="https://github.com/crdoconnor/strictyaml/blob/master/hitch/story/boolean.story">boolean.story
    storytests.