def _parse_index( index: Optional[Union[int, list, tuple, slice, str]] = None ) -> Optional[Union[int, slice]]: if index is None: return None elif isinstance(index, (int, slice)): return index elif isinstance(index, (list, tuple)): if len(index) > 3: raise ge_exceptions.BatchFilterError( f"""The number of index slice components must be between 1 and 3 (the given number is {len(index)}). """) if len(index) == 1: return index[0] if len(index) == 2: return slice(index[0], index[1], None) if len(index) == 3: return slice(index[0], index[1], index[2]) elif isinstance(index, str): if is_int(value=index): return _parse_index(index=int(index)) return _parse_index( index=[int(idx_str) for idx_str in index.split(":")]) else: raise ge_exceptions.BatchFilterError( f"""The type of index must be an integer (Python "int"), or a list (Python "list") or a tuple (Python "tuple"), or a Python "slice" object, or a string that has the format of a single integer or a slice argument. The type given is "{str(type(index))}", which is illegal. """)
def build_batch_filter(data_connector_query_dict: Optional[Dict[ str, Optional[Union[int, list, tuple, slice, str, Union[Dict, IDDict], Callable, ]], ]] = None): if not data_connector_query_dict: return BatchFilter( custom_filter_function=None, batch_filter_parameters=None, index=None, limit=None, ) data_connector_query_keys: set = set(data_connector_query_dict.keys()) if not data_connector_query_keys <= BatchFilter.RECOGNIZED_KEYS: raise ge_exceptions.BatchFilterError( f"""Unrecognized data_connector_query key(s): "{str(data_connector_query_keys - BatchFilter.RECOGNIZED_KEYS)}" detected. """) custom_filter_function: Callable = data_connector_query_dict.get( "custom_filter_function") if custom_filter_function and not isinstance(custom_filter_function, Callable): raise ge_exceptions.BatchFilterError( f"""The type of a custom_filter must be a function (Python "Callable"). The type given is "{str(type(custom_filter_function))}", which is illegal. """) batch_filter_parameters: Optional[dict] = data_connector_query_dict.get( "batch_filter_parameters") if batch_filter_parameters: if not isinstance(batch_filter_parameters, dict): raise ge_exceptions.BatchFilterError( f"""The type of batch_filter_parameters must be a dictionary (Python "dict"). The type given is "{str(type(batch_filter_parameters))}", which is illegal. """) if not all( [isinstance(key, str) for key in batch_filter_parameters.keys()]): raise ge_exceptions.BatchFilterError( 'All batch_filter_parameters keys must strings (Python "str").' ) if batch_filter_parameters is not None: batch_filter_parameters: IDDict = IDDict(batch_filter_parameters) index: Optional[Union[int, list, tuple, slice, str]] = data_connector_query_dict.get("index") limit: Optional[int] = data_connector_query_dict.get("limit") if limit and (not isinstance(limit, int) or limit < 0): raise ge_exceptions.BatchFilterError( f"""The type of a limit must be an integer (Python "int") that is greater than or equal to 0. The type and value given are "{str(type(limit))}" and "{limit}", respectively, which is illegal. """) if index is not None and limit is not None: raise ge_exceptions.BatchFilterError( "Only one of index or limit, but not both, can be specified (specifying both is illegal)." ) index = _parse_index(index=index) return BatchFilter( custom_filter_function=custom_filter_function, batch_filter_parameters=batch_filter_parameters, index=index, limit=limit, )