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
|
---
title: Integers (Int)
---
StrictYAML parses to a YAML object, not
the value directly to give you more flexibility
and control over what you can do with the YAML.
This is what that can object can do - in many
cases if parsed as a integer, it will behave in
the same way.
Example yaml_snippet:
```yaml
a: 1
b: 5
```
```python
from strictyaml import Map, Int, load
from ensure import Ensure
schema = Map({"a": Int(), "b": Int()})
parsed = load(yaml_snippet, schema)
```
Parsed correctly:
```python
Ensure(parsed).equals({"a": 1, "b": 5})
```
Has underscores:
```yaml
a: 10_000_000
b: 10_0_0
```
```python
Ensure(load(yaml_snippet, schema).data).equals({"a": 10000000, "b": 1000})
```
Cast with str:
```python
Ensure(str(parsed["a"])).equals("1")
```
Cast with float:
```python
Ensure(float(parsed["a"])).equals(1.0)
```
Greater than:
```python
Ensure(parsed["a"] > 0).equals(True)
```
Less than:
```python
Ensure(parsed["a"] < 2).equals(True)
```
To get actual int, use .data:
```python
Ensure(type(load(yaml_snippet, schema)["a"].data) is int).equals(True)
```
Cannot cast to bool:
```python
bool(load(yaml_snippet, schema)['a'])
```
```python
:
Cannot cast 'YAML(1)' to bool.
Use bool(yamlobj.data) or bool(yamlobj.text) instead.
```
!!! note "Executable specification"
Documentation automatically generated from
<a href="https://github.com/crdoconnor/strictyaml/blob/master/hitch/story/scalar-integer.story">scalar-integer.story
storytests.
|