def from_str(cls, feature_ref_str: str, ignore_project: bool = False): """ Parse the given string feature reference into FeatureRef model String feature reference should be in the format feature_set:feature. Where "feature_set" and "name" are the feature_set name and feature name respectively. Args: feature_ref_str: String representation of the feature reference ignore_project: Ignore projects in given string feature reference instead throwing an error Returns: FeatureRef that refers to the given feature """ proto = FeatureRefProto() if "/" in feature_ref_str: if ignore_project: _, feature_ref_str = feature_ref_str.split("/") else: raise ValueError( f"Unsupported feature reference: {feature_ref_str}") # parse feature set name if specified if ":" in feature_ref_str: proto.feature_set, feature_ref_str = feature_ref_str.split(":") proto.name = feature_ref_str return cls.from_proto(proto)
def _build_feature_references( feature_refs: List[str], default_project: str = None) -> List[FeatureReference]: """ Builds a list of FeatureSet objects from feature set ids in order to retrieve feature data from Feast Serving Args: feature_refs: List of feature reference strings ("project/feature:version") default_project: This project will be used if the project name is not provided in the feature reference """ features = [] for feature_ref in feature_refs: project_split = feature_ref.split("/") version = 0 if len(project_split) == 2: project, feature_version = project_split elif len(project_split) == 1: feature_version = project_split[0] if default_project is None: raise ValueError( f"No project specified in {feature_ref} and no default project provided" ) project = default_project else: raise ValueError( f'Could not parse feature ref {feature_ref}, expecting "project/feature:version"' ) feature_split = feature_version.split(":") if len(feature_split) == 2: name, version = feature_split version = int(version) elif len(feature_split) == 1: name = feature_split[0] else: raise ValueError( f'Could not parse feature ref {feature_ref}, expecting "project/feature:version"' ) if len(project) == 0 or len(name) == 0 or version < 0: raise ValueError( f'Could not parse feature ref {feature_ref}, expecting "project/feature:version"' ) features.append( FeatureReference(project=project, name=name, version=version)) return features