|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import operator |
| 4 | +from typing import SupportsFloat, SupportsIndex |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import numpy.typing as npt |
| 8 | + |
| 9 | + |
| 10 | +def arg_to_float( |
| 11 | + arg_description: str, value: SupportsFloat | None, default_value: float | None = None |
| 12 | +) -> float: |
| 13 | + """Convert an argument to a float. |
| 14 | +
|
| 15 | + >>> arg_to_float("xyz", 1.234) |
| 16 | + 1.234 |
| 17 | + >>> arg_to_float("xyz", 1234) |
| 18 | + 1234.0 |
| 19 | + >>> arg_to_float("xyz", np.float64(1.234)) |
| 20 | + np.float64(1.234) |
| 21 | + >>> arg_to_float("xyz", np.float32(1.234)) # doctest: +ELLIPSIS |
| 22 | + 1.233999... |
| 23 | + >>> arg_to_float("xyz", 1.234, 5.0) |
| 24 | + 1.234 |
| 25 | + >>> arg_to_float("xyz", None, 5.0) |
| 26 | + 5.0 |
| 27 | + >>> arg_to_float("xyz", None) |
| 28 | + Traceback (most recent call last): |
| 29 | + ... |
| 30 | + TypeError: The xyz must be a floating point number. |
| 31 | + <BLANKLINE> |
| 32 | + Provided value: None |
| 33 | + >>> arg_to_float("xyz", "1.234") |
| 34 | + Traceback (most recent call last): |
| 35 | + ... |
| 36 | + TypeError: The xyz must be a floating point number. |
| 37 | + <BLANKLINE> |
| 38 | + Provided value: '1.234' |
| 39 | + """ |
| 40 | + if value is None: |
| 41 | + if default_value is None: |
| 42 | + raise TypeError( |
| 43 | + f"The {arg_description} must be a floating point number.\n\n" |
| 44 | + f"Provided value: {value!r}" |
| 45 | + ) |
| 46 | + value = default_value |
| 47 | + |
| 48 | + if not isinstance(value, float): |
| 49 | + try: |
| 50 | + # Use value.__float__() because float(value) also accepts strings. |
| 51 | + return value.__float__() |
| 52 | + except Exception: |
| 53 | + raise TypeError( |
| 54 | + f"The {arg_description} must be a floating point number.\n\n" |
| 55 | + f"Provided value: {value!r}" |
| 56 | + ) from None |
| 57 | + |
| 58 | + return value |
| 59 | + |
| 60 | + |
| 61 | +def arg_to_int( |
| 62 | + arg_description: str, value: SupportsIndex | None, default_value: int | None = None |
| 63 | +) -> int: |
| 64 | + """Convert an argument to a signed integer. |
| 65 | +
|
| 66 | + >>> arg_to_int("xyz", 1234) |
| 67 | + 1234 |
| 68 | + >>> arg_to_int("xyz", 1234, -1) |
| 69 | + 1234 |
| 70 | + >>> arg_to_int("xyz", None, -1) |
| 71 | + -1 |
| 72 | + >>> arg_to_int("xyz", None) |
| 73 | + Traceback (most recent call last): |
| 74 | + ... |
| 75 | + TypeError: The xyz must be an integer. |
| 76 | + <BLANKLINE> |
| 77 | + Provided value: None |
| 78 | + >>> arg_to_int("xyz", 1.234) |
| 79 | + Traceback (most recent call last): |
| 80 | + ... |
| 81 | + TypeError: The xyz must be an integer. |
| 82 | + <BLANKLINE> |
| 83 | + Provided value: 1.234 |
| 84 | + >>> arg_to_int("xyz", "1234") |
| 85 | + Traceback (most recent call last): |
| 86 | + ... |
| 87 | + TypeError: The xyz must be an integer. |
| 88 | + <BLANKLINE> |
| 89 | + Provided value: '1234' |
| 90 | + """ |
| 91 | + if value is None: |
| 92 | + if default_value is None: |
| 93 | + raise TypeError( |
| 94 | + f"The {arg_description} must be an integer.\n\n" f"Provided value: {value!r}" |
| 95 | + ) |
| 96 | + value = default_value |
| 97 | + |
| 98 | + if not isinstance(value, int): |
| 99 | + try: |
| 100 | + return operator.index(value) |
| 101 | + except Exception: |
| 102 | + raise TypeError( |
| 103 | + f"The {arg_description} must be an integer.\n\n" f"Provided value: {value!r}" |
| 104 | + ) from None |
| 105 | + |
| 106 | + return value |
| 107 | + |
| 108 | + |
| 109 | +def arg_to_uint( |
| 110 | + arg_description: str, value: SupportsIndex | None, default_value: int | None = None |
| 111 | +) -> int: |
| 112 | + """Convert an argument to an unsigned integer. |
| 113 | +
|
| 114 | + >>> arg_to_uint("xyz", 1234) |
| 115 | + 1234 |
| 116 | + >>> arg_to_uint("xyz", 1234, 5000) |
| 117 | + 1234 |
| 118 | + >>> arg_to_uint("xyz", None, 5000) |
| 119 | + 5000 |
| 120 | + >>> arg_to_uint("xyz", -1234) |
| 121 | + Traceback (most recent call last): |
| 122 | + ... |
| 123 | + ValueError: The xyz must be a non-negative integer. |
| 124 | + <BLANKLINE> |
| 125 | + Provided value: -1234 |
| 126 | + >>> arg_to_uint("xyz", "1234") |
| 127 | + Traceback (most recent call last): |
| 128 | + ... |
| 129 | + TypeError: The xyz must be an integer. |
| 130 | + <BLANKLINE> |
| 131 | + Provided value: '1234' |
| 132 | + """ |
| 133 | + value = arg_to_int(arg_description, value, default_value) |
| 134 | + if value < 0: |
| 135 | + raise ValueError( |
| 136 | + f"The {arg_description} must be a non-negative integer.\n\n" |
| 137 | + f"Provided value: {value!r}" |
| 138 | + ) |
| 139 | + return value |
| 140 | + |
| 141 | + |
| 142 | +def validate_dtype(dtype: npt.DTypeLike, supported_dtypes: tuple[npt.DTypeLike, ...]) -> None: |
| 143 | + """Validate a dtype-like object against a tuple of supported dtype-like objects. |
| 144 | +
|
| 145 | + >>> validate_dtype(np.float64, (np.float64, np.intc, np.long,)) |
| 146 | + >>> validate_dtype("float64", (np.float64, np.intc, np.long,)) |
| 147 | + >>> validate_dtype(np.float64, (np.byte, np.short, np.intc, np.int_, np.long, np.longlong)) |
| 148 | + Traceback (most recent call last): |
| 149 | + ... |
| 150 | + TypeError: The requested data type is not supported. |
| 151 | + <BLANKLINE> |
| 152 | + Data type: float64 |
| 153 | + Supported data types: int8, int16, int32, int64 |
| 154 | + """ |
| 155 | + if not isinstance(dtype, (type, np.dtype)): |
| 156 | + dtype = np.dtype(dtype) |
| 157 | + if not np.isdtype(dtype, supported_dtypes): |
| 158 | + # Remove duplicate names because distinct types (e.g. int vs. long) may have the same name |
| 159 | + # ("int32"). |
| 160 | + supported_dtype_names = {np.dtype(d).name: None for d in supported_dtypes}.keys() |
| 161 | + raise TypeError( |
| 162 | + "The requested data type is not supported.\n\n" |
| 163 | + f"Data type: {np.dtype(dtype)}\n" |
| 164 | + f"Supported data types: {', '.join(supported_dtype_names)}" |
| 165 | + ) |
0 commit comments