pyvista.core._validation.check.check_length#
- check_length(
- sized_input: float | Sized,
- /,
- exact_length: int | VectorLike[int] | None = None,
- *,
- min_length: int | None = None,
- max_length: int | None = None,
- must_be_1d: bool = False,
- allow_scalar: bool = False,
- name: str = 'Array',
Check if the length of an array meets specific requirements.
- Parameters:
- sized_input
float|Sized Number or array to check.
- exact_length
int|VectorLike[int],optional Check if the array has the given length. If multiple numbers are given, the array’s length must match one of the numbers.
- min_length
int,optional Check if the array has this length or greater.
- max_length
int,optional Check if the array has this length or less.
- must_be_1dbool, default:
False If
True, check if the shape of the array is one-dimensional, i.e. that the array’s shape is(1,).- allow_scalarbool, default:
False If
True, a scalar input will be reshaped to have a length of 1. Otherwise, the check will fail since a scalar does not have a length.- name
str, default: “Array” Variable name to use in the error messages if any are raised.
- sized_input
- Raises:
ValueErrorIf the array’s length is outside the specified range.
See also
Notes
By default, this function operates on multidimensional arrays, where
len(arr)may differ from the number of elements in the array. For one-dimensional cases (wherelen(arr) == arr.size), setmust_be_1D=True.Examples
Check if an array has a length of 2 or 3.
>>> from pyvista import _validation >>> _validation.check_length([1, 2], exact_length=[2, 3])
Check if an array has a minimum length of 3.
>>> _validation.check_length((1, 2, 3), min_length=3)
Check if a multidimensional array has a maximum length of 2.
>>> _validation.check_length([[1, 2, 3], [4, 5, 6]], max_length=2)