Floating point numbers (Float)
StrictYAML parses to a YAML object representing a decimal - e.g. YAML(1.0000000000000001)
To get a python float literal, use .data.
Parsing and validating as a Decimal is best for values which require precision, but float is better for values for which precision is not required.
Example yaml_snippet:
a: 1.00000000000000000001
b: 5.4135
from math import isnan, isinf
from strictyaml import Map, MapPattern, Str, Float, Bool, load, as_document
from collections import OrderedDict
from ensure import Ensure
schema = Map({"a": Float(), "b": Float()})
Use .data to get float type:
Ensure(type(load(yaml_snippet, schema)["a"].data)).equals(float)
Equal to equivalent float which is different number:
Ensure(load(yaml_snippet, schema)).equals({"a": 1.0, "b": 5.4135})
Cast to str:
Ensure(str(load(yaml_snippet, schema)["a"])).equals("1.0")
Cast to float:
Ensure(float(load(yaml_snippet, schema)["a"])).equals(1.0)
Greater than:
Ensure(load(yaml_snippet, schema)["a"] > 0).is_true()
Less than:
Ensure(load(yaml_snippet, schema)["a"] < 0).is_false()
Has NaN values:
a: nan
b: .NaN
Ensure(isnan(load(yaml_snippet, schema)["a"].data)).is_true()
Ensure(isnan(load(yaml_snippet, schema)["b"].data)).is_true()
Has infinity values:
a: -.Inf
b: INF
Ensure(isinf(load(yaml_snippet, schema)["a"].data)).is_true()
Ensure(isinf(load(yaml_snippet, schema)["b"].data)).is_true()
Has underscores:
a: 10_000_000.5
b: 10_0_0.2_5
Ensure(load(yaml_snippet, schema).data).equals({"a": 10000000.5, "b": 1000.25})
Cannot cast to bool:
bool(load(yaml_snippet, schema)['a'])
:
Cannot cast 'YAML(1.0)' to bool.
Use bool(yamlobj.data) or bool(yamlobj.text) instead.
Cannot parse non-float:
a: string
b: 2
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting a float
found arbitrary text
in "<unicode string>", line 1, column 1:
a: string
^ (line: 1)
Serialize successfully:
print(as_document(OrderedDict([("a", 3.5), ("b", "2.1")]), schema).as_yaml())
a: 3.5
b: 2.1
Serialize successfully with NaN:
print(as_document(OrderedDict([("a", 3.5), ("b", float("nan"))]), schema).as_yaml())
a: 3.5
b: nan
Serialize successfully with infinity:
print(as_document(OrderedDict([("a", float("inf")), ("b", float("-inf"))]), schema).as_yaml())
a: inf
b: -inf
Serialization failure:
as_document(OrderedDict([("a", "x"), ("b", "2.1")]), schema)
strictyaml.exceptions.YAMLSerializationError:
when expecting a float, got 'x'
Float as key:
document = as_document(OrderedDict([("3.5", "a"), ("2.1", "c")]), MapPattern(Float(), Str()))
print(document.data[3.5])
print(document.data[2.1])
a
c
Float or bool:
document = as_document({"a": True}, Map({"a": Float() | Bool()}))
print(document.as_yaml())
a: yes
Executable specification
Documentation automatically generated from float.story storytests.