| 1 |
Floating point numbers (Float): |
| 2 |
docs: scalar/float |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
StrictYAML parses to a YAML object representing |
| 6 |
a decimal - e.g. YAML(1.0000000000000001) |
| 7 |
|
| 8 |
To get a python float literal, use .data. |
| 9 |
|
| 10 |
Parsing and validating as a Decimal is best for |
| 11 |
values which require precision, but float is better |
| 12 |
for values for which precision is not required. |
| 13 |
given: |
| 14 |
setup: | |
| 15 |
from math import isnan, isinf |
| 16 |
|
| 17 |
from strictyaml import Map, MapPattern, Str, Float, Bool, load, as_document |
| 18 |
from collections import OrderedDict |
| 19 |
from ensure import Ensure |
| 20 |
|
| 21 |
schema = Map({"a": Float(), "b": Float()}) |
| 22 |
|
| 23 |
yaml_snippet: | |
| 24 |
a: 1.00000000000000000001 |
| 25 |
b: 5.4135 |
| 26 |
variations: |
| 27 |
Use .data to get float type: |
| 28 |
steps: |
| 29 |
- Run: |
| 30 |
code: | |
| 31 |
Ensure(type(load(yaml_snippet, schema)["a"].data)).equals(float) |
| 32 |
|
| 33 |
Equal to equivalent float which is different number: |
| 34 |
steps: |
| 35 |
- Run: |
| 36 |
code: | |
| 37 |
Ensure(load(yaml_snippet, schema)).equals({"a": 1.0, "b": 5.4135}) |
| 38 |
|
| 39 |
Cast to str: |
| 40 |
steps: |
| 41 |
- Run: |
| 42 |
code: | |
| 43 |
Ensure(str(load(yaml_snippet, schema)["a"])).equals("1.0") |
| 44 |
|
| 45 |
Cast to float: |
| 46 |
steps: |
| 47 |
- Run: |
| 48 |
code: | |
| 49 |
Ensure(float(load(yaml_snippet, schema)["a"])).equals(1.0) |
| 50 |
|
| 51 |
Greater than: |
| 52 |
steps: |
| 53 |
- Run: |
| 54 |
code: | |
| 55 |
Ensure(load(yaml_snippet, schema)["a"] > 0).is_true() |
| 56 |
|
| 57 |
Less than: |
| 58 |
steps: |
| 59 |
- Run: |
| 60 |
code: | |
| 61 |
Ensure(load(yaml_snippet, schema)["a"] < 0).is_false() |
| 62 |
|
| 63 |
Has NaN values: |
| 64 |
given: |
| 65 |
yaml_snippet: | |
| 66 |
a: nan |
| 67 |
b: .NaN |
| 68 |
steps: |
| 69 |
- Run: |
| 70 |
code: | |
| 71 |
Ensure(isnan(load(yaml_snippet, schema)["a"].data)).is_true() |
| 72 |
Ensure(isnan(load(yaml_snippet, schema)["b"].data)).is_true() |
| 73 |
|
| 74 |
Has infinity values: |
| 75 |
given: |
| 76 |
yaml_snippet: | |
| 77 |
a: -.Inf |
| 78 |
b: INF |
| 79 |
steps: |
| 80 |
- Run: |
| 81 |
code: | |
| 82 |
Ensure(isinf(load(yaml_snippet, schema)["a"].data)).is_true() |
| 83 |
Ensure(isinf(load(yaml_snippet, schema)["b"].data)).is_true() |
| 84 |
|
| 85 |
Has underscores: |
| 86 |
given: |
| 87 |
yaml_snippet: | |
| 88 |
a: 10_000_000.5 |
| 89 |
b: 10_0_0.2_5 |
| 90 |
steps: |
| 91 |
- Run: |
| 92 |
code: | |
| 93 |
Ensure(load(yaml_snippet, schema).data).equals({"a": 10000000.5, "b": 1000.25}) |
| 94 |
|
| 95 |
Cannot cast to bool: |
| 96 |
steps: |
| 97 |
- Run: |
| 98 |
code: bool(load(yaml_snippet, schema)['a']) |
| 99 |
raises: |
| 100 |
message: |- |
| 101 |
Cannot cast 'YAML(1.0)' to bool. |
| 102 |
Use bool(yamlobj.data) or bool(yamlobj.text) instead. |
| 103 |
|
| 104 |
Cannot parse non-float: |
| 105 |
given: |
| 106 |
yaml_snippet: | |
| 107 |
a: string |
| 108 |
b: 2 |
| 109 |
steps: |
| 110 |
- Run: |
| 111 |
code: load(yaml_snippet, schema) |
| 112 |
raises: |
| 113 |
type: strictyaml.exceptions.YAMLValidationError |
| 114 |
message: |- |
| 115 |
when expecting a float |
| 116 |
found arbitrary text |
| 117 |
in "<unicode string>", line 1, column 1: |
| 118 |
a: string |
| 119 |
^ (line: 1) |
| 120 |
|
| 121 |
Serialize successfully: |
| 122 |
steps: |
| 123 |
- Run: |
| 124 |
code: print(as_document(OrderedDict([("a", 3.5), ("b", "2.1")]), schema).as_yaml()) |
| 125 |
will output: |- |
| 126 |
a: 3.5 |
| 127 |
b: 2.1 |
| 128 |
|
| 129 |
Serialize successfully with NaN: |
| 130 |
steps: |
| 131 |
- Run: |
| 132 |
code: print(as_document(OrderedDict([("a", 3.5), ("b", float("nan"))]), schema).as_yaml()) |
| 133 |
will output: |- |
| 134 |
a: 3.5 |
| 135 |
b: nan |
| 136 |
|
| 137 |
Serialize successfully with infinity: |
| 138 |
steps: |
| 139 |
- Run: |
| 140 |
code: print(as_document(OrderedDict([("a", float("inf")), ("b", float("-inf"))]), schema).as_yaml()) |
| 141 |
will output: |- |
| 142 |
a: inf |
| 143 |
b: -inf |
| 144 |
|
| 145 |
Serialization failure: |
| 146 |
steps: |
| 147 |
- Run: |
| 148 |
code: as_document(OrderedDict([("a", "x"), ("b", "2.1")]), schema) |
| 149 |
raises: |
| 150 |
type: strictyaml.exceptions.YAMLSerializationError |
| 151 |
message: when expecting a float, got 'x' |
| 152 |
|
| 153 |
Float as key: |
| 154 |
steps: |
| 155 |
- Run: |
| 156 |
code: | |
| 157 |
document = as_document(OrderedDict([("3.5", "a"), ("2.1", "c")]), MapPattern(Float(), Str())) |
| 158 |
print(document.data[3.5]) |
| 159 |
print(document.data[2.1]) |
| 160 |
will output: |- |
| 161 |
a |
| 162 |
c |
| 163 |
|
| 164 |
Float or bool: |
| 165 |
steps: |
| 166 |
- Run: |
| 167 |
code: | |
| 168 |
document = as_document({"a": True}, Map({"a": Float() | Bool()})) |
| 169 |
print(document.as_yaml()) |
| 170 |
will output: |- |
| 171 |
a: yes |