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 strictyaml import Map, Float, 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()

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)
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

Serialization failure:

as_document(OrderedDict([("a", "x"), ("b", "2.1")]), schema)
when expecting a float, got 'x'

Executable specification

Page automatically generated from float.story.