predictably.validate.is_sequence_named_objects#
- predictably.validate.is_sequence_named_objects(seq_to_check: Sequence[Tuple[str, BaseObject]] | Dict[str, BaseObject], allow_dict: bool = True, require_unique_names: bool = False, object_type: type | Tuple[type] | None = None) bool[source]#
Indicate if input is a sequence of named BaseObject instances.
This can be a sequence of (str, BaseObject instance) tuples or a dictionary with string names as keys and BaseObject instances as values (if
allow_dict=True).- Parameters:
- seq_to_checkSequence((str, BaseObject)) or Dict[str, BaseObject]
The input to check for conformance with the named object interface. Conforming input are:
Sequence that contains (str, BaseObject instance) tuples
Dictionary with string names as keys and BaseObject instances as values if
allow_dict=True
- allow_dictbool, default=True
Whether a dictionary of named objects is allowed as conforming named object type.
If True, then a dictionary with string keys and BaseObject instances is allowed format for providing a sequence of named objects.
If False, then only sequences that contain (str, BaseObject instance) tuples are considered conforming with the named object parameter API.
- require_unique_namesbool, default=False
Whether names used in the sequence of named BaseObject instances must be unique.
If True and the names are not unique, then False is always returned.
If False, then whether or not the function returns True or False depends on whether seq_to_check follows sequence of named BaseObject format.
- object_typeclass or tuple[class], default=None
The class type(s) that is used to ensure that all elements of named objects match the expected type.
- Returns:
- bool
Whether the input seq_to_check is a sequence that follows the API for nameed base object instances.
- Raises:
- ValueError
If seq_to_check is not a sequence or
allow_dict is Falseand seq_to_check is a dictionary.
Examples
>>> from predictably._core._base import BaseObject, BaseEstimator >>> from predictably.validate import is_sequence_named_objects >>> named_objects = [("Step 1", BaseObject()), ("Step 2", BaseObject())] >>> is_sequence_named_objects(named_objects) True
Dictionaries are optionally allowed as sequences of named BaseObjects
>>> dict_named_objects = {"Step 1": BaseObject(), "Step 2": BaseObject()} >>> is_sequence_named_objects(dict_named_objects) True >>> is_sequence_named_objects(dict_named_objects, allow_dict=False) False
Invalid format due to object names not being strings
>>> incorrectly_named_objects = [(1, BaseObject()), (2, BaseObject())] >>> is_sequence_named_objects(incorrectly_named_objects) False
Invalid format due to named items not being BaseObject instances
>>> named_items = [("1", 7), ("2", 42)] >>> is_sequence_named_objects(named_items) False
The validation can require the object elements to be a certain class type
>>> named_objects = [("Step 1", BaseObject()), ("Step 2", BaseObject())] >>> is_sequence_named_objects(named_objects, object_type=BaseEstimator) False >>> named_objects = [("Step 1", BaseEstimator()), ("Step 2", BaseEstimator())] >>> is_sequence_named_objects(named_objects, object_type=BaseEstimator) True