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:

a: 1
b: 5
from strictyaml import Map, Int, load
from ensure import Ensure

schema = Map({"a": Int(), "b": Int()})

parsed = load(yaml_snippet, schema)

Parsed correctly:

Ensure(parsed).equals({"a": 1, "b": 5})

Cast with str:

Ensure(str(parsed["a"])).equals("1")

Cast with float:

Ensure(float(parsed["a"])).equals(1.0)

Greater than:

Ensure(parsed["a"] > 0).equals(True)

Less than:

Ensure(parsed["a"] < 2).equals(True)

To get actual int, use .data:

Ensure(type(load(yaml_snippet, schema)["a"].data) is int).equals(True)

Cannot cast to bool:

bool(load(yaml_snippet, schema)['a'])
:
Cannot cast 'YAML(1)' to bool.
Use bool(yamlobj.data) or bool(yamlobj.text) instead.

Executable specification

Page automatically generated from scalar-integer.story.