forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfield.py
More file actions
129 lines (109 loc) · 4.02 KB
/
field.py
File metadata and controls
129 lines (109 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright 2022 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, Optional
from pydantic import BaseModel, ConfigDict, field_validator
from typeguard import check_type, typechecked
from feast.feature import Feature
from feast.protos.feast.core.Feature_pb2 import FeatureSpecV2 as FieldProto
from feast.types import FeastType, from_string, from_value_type
from feast.value_type import ValueType
@typechecked
class Field(BaseModel):
"""
A Field represents a set of values with the same structure.
Attributes:
name: The name of the field.
dtype: The type of the field, such as string or float.
description: A human-readable description.
tags: User-defined metadata in dictionary form.
"""
model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")
name: str
dtype: FeastType
description: str = ""
tags: Optional[Dict[str, str]] = {}
@field_validator("dtype", mode="before")
def dtype_is_feasttype_or_string_feasttype(cls, v):
"""
dtype must be a FeastType, but to allow wire transmission,
it is necessary to allow string representations of FeastTypes.
We therefore allow dtypes to be specified as strings which are
converted to FeastTypes at time of definition.
TO-DO: Investigate whether FeastType can be refactored to a json compatible
format.
"""
try:
check_type(v, FeastType) # type: ignore
except TypeError:
try:
check_type(v, str)
return from_string(v)
except TypeError:
raise TypeError("dtype must be of type FeastType")
return v
def __eq__(self, other):
if type(self) is not type(other):
return False
if (
self.name != other.name
or self.dtype != other.dtype
or self.description != other.description
or self.tags != other.tags
):
return False
return True
def __hash__(self):
return hash((self.name, hash(self.dtype)))
def __lt__(self, other):
return self.name < other.name
def __repr__(self):
return f"{self.name}-{self.dtype}"
def __str__(self):
return f"Field(name={self.name}, dtype={self.dtype}, tags={self.tags})"
def to_proto(self) -> FieldProto:
"""Converts a Field object to its protobuf representation."""
value_type = self.dtype.to_value_type()
return FieldProto(
name=self.name,
value_type=value_type.value,
description=self.description,
tags=self.tags,
)
@classmethod
def from_proto(cls, field_proto: FieldProto):
"""
Creates a Field object from a protobuf representation.
Args:
field_proto: FieldProto protobuf object
"""
value_type = ValueType(field_proto.value_type)
return cls(
name=field_proto.name,
dtype=from_value_type(value_type=value_type),
tags=dict(field_proto.tags),
description=field_proto.description,
)
@classmethod
def from_feature(cls, feature: Feature):
"""
Creates a Field object from a Feature object.
Args:
feature: Feature object to convert.
"""
return cls(
name=feature.name,
dtype=from_value_type(feature.dtype),
description=feature.description,
tags=feature.labels,
)