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
|
Floating point numbers (Float):
docs: scalar/float
based on: strictyaml
description: |
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.
given:
setup: |
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()})
yaml_snippet: |
a: 1.00000000000000000001
b: 5.4135
variations:
Use .data to get float type:
steps:
- Run:
code: |
Ensure(type(load(yaml_snippet, schema)["a"].data)).equals(float)
Equal to equivalent float which is different number:
steps:
- Run:
code: |
Ensure(load(yaml_snippet, schema)).equals({"a": 1.0, "b": 5.4135})
Cast to str:
steps:
- Run:
code: |
Ensure(str(load(yaml_snippet, schema)["a"])).equals("1.0")
Cast to float:
steps:
- Run:
code: |
Ensure(float(load(yaml_snippet, schema)["a"])).equals(1.0)
Greater than:
steps:
- Run:
code: |
Ensure(load(yaml_snippet, schema)["a"] > 0).is_true()
Less than:
steps:
- Run:
code: |
Ensure(load(yaml_snippet, schema)["a"] < 0).is_false()
Has NaN values:
given:
yaml_snippet: |
a: nan
b: .NaN
steps:
- Run:
code: |
Ensure(isnan(load(yaml_snippet, schema)["a"].data)).is_true()
Ensure(isnan(load(yaml_snippet, schema)["b"].data)).is_true()
Has infinity values:
given:
yaml_snippet: |
a: -.Inf
b: INF
steps:
- Run:
code: |
Ensure(isinf(load(yaml_snippet, schema)["a"].data)).is_true()
Ensure(isinf(load(yaml_snippet, schema)["b"].data)).is_true()
Has underscores:
given:
yaml_snippet: |
a: 10_000_000.5
b: 10_0_0.2_5
steps:
- Run:
code: |
Ensure(load(yaml_snippet, schema).data).equals({"a": 10000000.5, "b": 1000.25})
Cannot cast to bool:
steps:
- Run:
code: bool(load(yaml_snippet, schema)['a'])
raises:
message: |-
Cannot cast 'YAML(1.0)' to bool.
Use bool(yamlobj.data) or bool(yamlobj.text) instead.
Cannot parse non-float:
given:
yaml_snippet: |
a: string
b: 2
steps:
- Run:
code: load(yaml_snippet, schema)
raises:
type: strictyaml.exceptions.YAMLValidationError
message: |-
when expecting a float
found arbitrary text
in "<unicode string>", line 1, column 1:
a: string
^ (line: 1)
Serialize successfully:
steps:
- Run:
code: print(as_document(OrderedDict([("a", 3.5), ("b", "2.1")]), schema).as_yaml())
will output: |-
a: 3.5
b: 2.1
Serialize successfully with NaN:
steps:
- Run:
code: print(as_document(OrderedDict([("a", 3.5), ("b", float("nan"))]), schema).as_yaml())
will output: |-
a: 3.5
b: nan
Serialize successfully with infinity:
steps:
- Run:
code: print(as_document(OrderedDict([("a", float("inf")), ("b", float("-inf"))]), schema).as_yaml())
will output: |-
a: inf
b: -inf
Serialization failure:
steps:
- Run:
code: as_document(OrderedDict([("a", "x"), ("b", "2.1")]), schema)
raises:
type: strictyaml.exceptions.YAMLSerializationError
message: when expecting a float, got 'x'
Float as key:
steps:
- Run:
code: |
document = as_document(OrderedDict([("3.5", "a"), ("2.1", "c")]), MapPattern(Float(), Str()))
print(document.data[3.5])
print(document.data[2.1])
will output: |-
a
c
Float or bool:
steps:
- Run:
code: |
document = as_document({"a": True}, Map({"a": Float() | Bool()}))
print(document.as_yaml())
will output: |-
a: yes
|