Returns a copy of `tup` with `dim` values by either shortened or duplicated input. Raises: ValueError: When ``tup`` is a sequence and ``tup`` length is not ``dim``. Examples:: >>> ensure_tuple_rep(1, 3) (1, 1, 1) >>> ensure_tuple_rep(None, 3) (
(tup: Any, dim: int)
| 192 | |
| 193 | |
| 194 | def ensure_tuple_rep(tup: Any, dim: int) -> tuple[Any, ...]: |
| 195 | """ |
| 196 | Returns a copy of `tup` with `dim` values by either shortened or duplicated input. |
| 197 | |
| 198 | Raises: |
| 199 | ValueError: When ``tup`` is a sequence and ``tup`` length is not ``dim``. |
| 200 | |
| 201 | Examples:: |
| 202 | |
| 203 | >>> ensure_tuple_rep(1, 3) |
| 204 | (1, 1, 1) |
| 205 | >>> ensure_tuple_rep(None, 3) |
| 206 | (None, None, None) |
| 207 | >>> ensure_tuple_rep('test', 3) |
| 208 | ('test', 'test', 'test') |
| 209 | >>> ensure_tuple_rep([1, 2, 3], 3) |
| 210 | (1, 2, 3) |
| 211 | >>> ensure_tuple_rep(range(3), 3) |
| 212 | (0, 1, 2) |
| 213 | >>> ensure_tuple_rep([1, 2], 3) |
| 214 | ValueError: Sequence must have length 3, got length 2. |
| 215 | |
| 216 | """ |
| 217 | if isinstance(tup, torch.Tensor): |
| 218 | tup = tup.detach().cpu().numpy() |
| 219 | if isinstance(tup, np.ndarray): |
| 220 | tup = tup.tolist() |
| 221 | if not issequenceiterable(tup): |
| 222 | return (tup,) * dim |
| 223 | if len(tup) == dim: |
| 224 | return tuple(tup) |
| 225 | |
| 226 | raise ValueError(f"Sequence must have length {dim}, got {len(tup)}.") |
| 227 | |
| 228 | |
| 229 | def to_tuple_of_dictionaries(dictionary_of_tuples: dict, keys: Any) -> tuple[dict[Any, Any], ...]: |
searching dependent graphs…