def guids_from_list_str(s: str) -> Optional[Tuple[str, ...]]: """ Get tuple of guids from a python/json string representation of a list. Extracts the guids from a string representation of a list, tuple, or set of guids or a single guid. Args: s: input string Returns: Extracted guids as a tuple of strings. If a provided string does not match the format, `None` will be returned. For an empty list/tuple/set or empty string an empty tuple is returned. Examples: >>> guids_from_str( "['07fd7195-c51e-44d6-a085-fa8274cf00d6', \ '070d7195-c51e-44d6-a085-fa8274cf00d6']") will return ('07fd7195-c51e-44d6-a085-fa8274cf00d6', '070d7195-c51e-44d6-a085-fa8274cf00d6') """ if s == "": return tuple() try: validate_guid_format(s) return (s,) except ValueError: pass try: parsed_expression = ast.parse(s, mode='eval') except SyntaxError: return None if not hasattr(parsed_expression, 'body'): return None parsed = cast(ast.Expression, parsed_expression).body if isinstance(parsed, ast.Str): if len(parsed.s) > 0: return (parsed.s,) else: return tuple() if not isinstance(parsed, (ast.List, ast.Tuple, ast.Set)): return None if not all(isinstance(e, ast.Str) for e in parsed.elts): return None str_elts = cast(Tuple[ast.Str, ...], tuple(parsed.elts)) return tuple(s.s for s in str_elts)
def get_dataset_by_identifier(identifier: Union[int, str]): """ returns a dataset for a given identifier identifier (str or int): run_id or guid """ if isinstance(identifier, int): dataset = load_by_run_spec(captured_run_id=identifier) elif isinstance(identifier, str): validate_guid_format(identifier) dataset = load_by_guid(identifier) return dataset
def validate_node(node_guid: str, node: str) -> None: """ Validate that the guid given is a valid guid. Args: node_guid: the guid node: either "head" or "tail" """ try: validate_guid_format(node_guid) except ValueError: raise ValueError( f'The guid given for {node} is not a valid guid. Received ' f'{node_guid}.')
def test_validation(): valid_guid = str(uuid4()) validate_guid_format(valid_guid) with pytest.raises(ValueError): validate_guid_format(valid_guid[1:])