skbase.validate.is_sequence#
- skbase.validate.is_sequence(input_seq: Any, sequence_type: type | Tuple[type, ...] | None = None, element_type: type | Tuple[type, ...] | None = None) bool[source]#
Indicate if an object is a sequence with optional check of element types.
If element_type is supplied all elements are also checked against provided types.
- Parameters:
- input_seqAny
The input sequence to be validated.
- sequence_typetype or tuple[type, …], default=None
The allowed sequence type(s) that input_seq can be an instance of.
If None, then collections.abc.Sequence is used (all sequence types are valid)
If sequence_type is a type or tuple of types, then only the specified types are considered valid.
- element_typetype or tuple[type], default=None
The allowed type(s) for elements of input_seq.
If None, then the elements of input_seq are not checked when determining if input_seq is a valid sequence.
If element_type is a type or tuple of types, then the elements of input_seq are checked to make sure they are all instances of the supplied element_type.
- Returns:
- bool
Whether the input is a valid sequence based on the supplied sequence_type and element_type.
Examples
>>> from skbase.base import BaseEstimator, BaseObject >>> from skbase.validate import is_sequence >>> is_sequence([1, 2, 3]) True >>> is_sequence(7) False
Generators are not sequences
>>> is_sequence((c for c in [1, 2, 3])) False
The expected sequence type can be included in the check
>>> is_sequence([1, 2, 3, 4], sequence_type=list) True >>> is_sequence([1, 2, 3, 4], sequence_type=tuple) False
The type of the elements can also be checked
>>> is_sequence([1, 2, 3], element_type=int) True >>> is_sequence([1, 2, 3, 4], sequence_type=list, element_type=int) True >>> is_sequence([1, 2, 3, 4], sequence_type=list, element_type=float) False >>> is_sequence([1, 2, 3, 4], sequence_type=list, element_type=(int, float)) True >>> is_sequence((BaseObject(), BaseEstimator()), element_type=BaseObject) True