pyvista.core._validation.check.check_length#

check_length(
arr,
/,
*,
exact_length=None,
min_length=None,
max_length=None,
must_be_1d=False,
allow_scalars=False,
name='Array',
)[source]#

Check if the length of an array meets specific requirements.

Parameters:
arrarray_like

Array to check.

exact_lengthint | array_like[int, …]

Check if the array has the given length. If multiple numbers are given, the array’s length must match one of the numbers.

min_lengthint, optional

Check if the array has this length or greater.

max_lengthint, 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_scalarsbool, 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.

namestr, default: “Array”

Variable name to use in the error messages if any are raised.

Raises:
ValueError

If the array’s length is outside the specified range.

See also

check_shape

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 (where len(arr) == arr.size), set must_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)