Errors
ts_stat_tests.utils.errors
🔗
Summary
Provides utility functions for generating standardized error messages and performing numeric assertions.
This module includes functions to format error messages consistently and check if numeric values are within a specified tolerance, which is useful for testing and validation purposes.
generate_error_message
🔗
generate_error_message(
parameter_name: str,
value_parsed: str,
options: Union[
Mapping[
str, Union[tuple[str, ...], list[str], str]
],
tuple[str, ...],
list[str],
],
) -> str
Summary
Generates a formatted error message for mismatched values or invalid options.
Details
This function constructs a standardized string that describes a mismatch between a provided value and the allowed options for a given parameter. It is primarily used to provide clear, consistent feedback in ValueError exceptions within dispatchers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameter_name
|
str
|
The name of the parameter or variable being checked. |
required |
value_parsed
|
str
|
The actual value that was received. |
required |
options
|
Union[Mapping[str, Union[tuple[str, ...], list[str], str]], tuple[str, ...], list[str]]
|
The set of valid options or a dictionary mapping categories to valid options. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A formatted error message string. |
Examples
| Setup | |
|---|---|
1 | |
| Example 1: Simple Options | |
|---|---|
1 2 3 | |
References
Source code in src/ts_stat_tests/utils/errors.py
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 | |
is_almost_equal
🔗
is_almost_equal(
first: float, second: float, *, places: int = 7
) -> bool
is_almost_equal(
first: float, second: float, *, delta: float
) -> bool
is_almost_equal(
first: float,
second: float,
*,
places: Optional[int] = None,
delta: Optional[float] = None
) -> bool
Summary
Checks if two float values are almost equal within a specified precision.
Details
Determines the equality of two floating-point numbers within a tolerance. This is necessary because floating-point arithmetic can introduce small errors that make direct equality comparisons (e.g., a == b) unreliable.
The user can specify tolerance either by places (decimal places) or by an absolute delta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
float
|
The first float value. |
required |
second
|
float
|
The second float value. |
required |
places
|
Optional[int]
|
The number of decimal places for comparison. Defaults to 7 if not provided. |
None
|
delta
|
Optional[float]
|
An optional delta value for comparison. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples
| Setup | |
|---|---|
1 | |
| Example 1: Using `places` | |
|---|---|
1 2 3 | |
| Example 2: Using `delta` | |
|---|---|
1 2 3 | |
| Example 3: Not Almost Equal | |
|---|---|
1 2 3 | |
Calculation
The comparison depends on whether delta or places is provided.
If delta is specified:
If places is specified (defaults to 7):
Credit
Inspiration from Python's UnitTest function assertAlmostEqual.
References
Source code in src/ts_stat_tests/utils/errors.py
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
assert_almost_equal
🔗
assert_almost_equal(
first: float,
second: float,
msg: Optional[str] = None,
*,
places: int = 7
) -> None
assert_almost_equal(
first: float,
second: float,
msg: Optional[str] = None,
*,
delta: float
) -> None
assert_almost_equal(
first: float,
second: float,
msg: Optional[str] = None,
*,
places: Optional[int] = None,
delta: Optional[float] = None
) -> None
Summary
Asserts that two float values are almost equal within a specified precision.
Details
Performs a floating-point comparison using is_almost_equal. If the comparison fails, an AssertionError is raised with a descriptive message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
float
|
The first float value. |
required |
second
|
float
|
The second float value. |
required |
msg
|
Optional[str]
|
An optional message to include in the exception if the values are not almost equal. |
None
|
places
|
Optional[int]
|
The number of decimal places for comparison. Defaults to 7 if not provided. |
None
|
delta
|
Optional[float]
|
An optional delta value for comparison. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
AssertionError
|
If the two float values are not almost equal. |
Returns:
| Type | Description |
|---|---|
None
|
None. Raises an |
Examples
| Setup | |
|---|---|
1 | |
| Example 1: Using `places` | |
|---|---|
1 2 3 | |
| Example 2: Using `delta` | |
|---|---|
1 2 3 | |
| Example 3: AssertionError Raised | |
|---|---|
1 2 3 4 | |
Calculation
Refer to is_almost_equal for the underlying logic.
Credit
Inspiration from Python's UnitTest function assertAlmostEqual.
References
Source code in src/ts_stat_tests/utils/errors.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |